Skip to content

Commit

Permalink
Context does not need to be mutable
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-kirby committed Jan 15, 2021
1 parent 0874d06 commit a86c88e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Context {
}
}

pub async fn make_dead(&mut self, target: &UserId) {
pub async fn make_dead(&self, target: &UserId) {
if let Some(game) = self.game.write().await.as_mut() {
if game.dead.insert(*target) && game.meeting_in_progress {
if let Err(why) = self
Expand Down Expand Up @@ -109,7 +109,7 @@ impl Context {
.map_or(false, |g| g.ctrl_msg == reaction.message_id)
}

pub async fn start_game(&mut self, msg: &Message, ctrl_user: UserId, guild_id: GuildId) {
pub async fn start_game(&self, msg: &Message, ctrl_user: UserId, guild_id: GuildId) {
self.game.write().await.replace(Game {
dead: HashSet::new(),
ctrl_channel: msg.channel_id,
Expand Down Expand Up @@ -225,7 +225,7 @@ impl Context {
Ok(())
}

pub async fn end_game(&mut self) -> Result<()> {
pub async fn end_game(&self) -> Result<()> {
if let Some(game) = self.game.write().await.take() {
self.discord_http
.delete_message(game.ctrl_channel, game.ctrl_msg)
Expand Down
57 changes: 27 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async fn main() -> Result<()> {

let mut events = shard.some_events(event_flags);

let mut context = Context::new(config, discord_http, cache, shutdown_handle, owners);
let context = Context::new(config, discord_http, cache, shutdown_handle, owners);

let parser = {
let mut parser_config = CommandParserConfig::new();
Expand Down Expand Up @@ -130,7 +130,7 @@ async fn main() -> Result<()> {
Ok(())
}

async fn process_command(mut ctx: Context, parser: Parser<'_>, msg: &Message) -> Result<()> {
async fn process_command(ctx: Context, parser: Parser<'_>, msg: &Message) -> Result<()> {
match parser.parse(&msg.content) {
Some(Command {
name: "new",
Expand Down Expand Up @@ -212,36 +212,33 @@ async fn process_command(mut ctx: Context, parser: Parser<'_>, msg: &Message) ->
.delete_message(msg.channel_id, msg.id)
.await?;

if ctx.is_in_control(&msg.author.id).await {
match arguments.next().map(UserId::parse) {
Some(Ok(target)) => {
let reply = ctx
.broadcast()
.await
.unwrap()
.content(format!("deadifying {}", target.mention()))?
.await?;
ctx.make_dead(&target).await;
sleep(Duration::from_secs(5)).await;
ctx.discord_http
.delete_message(reply.channel_id, reply.id)
.await?;
}
_ => {
ctx.broadcast()
.await
.unwrap()
.content("You must mention the user you wish to die")?
.await?;
if let Some(broadcast) = ctx.broadcast().await {
if ctx.is_in_control(&msg.author.id).await {
match arguments.next().map(UserId::parse) {
Some(Ok(target)) => {
let reply = broadcast
.content(format!("deadifying {}", target.mention()))?
.await?;
ctx.make_dead(&target).await;
sleep(Duration::from_secs(5)).await;
ctx.discord_http
.delete_message(reply.channel_id, reply.id)
.await?;
}
_ => {
broadcast
.content("You must mention the user you wish to die")?
.await?;
}
}
} else {
broadcast
.content(
"You must have started the game or be an owner of the bot to make \
others dead\nTo make yourself dead, please use the reactions",
)?
.await?;
}
} else if let Some(broadcast) = ctx.broadcast().await {
broadcast
.content(
"You must have started the game or be an owner of the bot to make others \
dead\nTo make yourself dead, please use the reactions",
)?
.await?;
} else {
ctx.discord_http
.create_message(msg.channel_id)
Expand Down

0 comments on commit a86c88e

Please sign in to comment.