Skip to content

Commit

Permalink
Replace &mut Context with &Context (#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
Th3-M4jor committed Apr 28, 2020
1 parent 9ad83c4 commit 3e4294b
Show file tree
Hide file tree
Showing 24 changed files with 98 additions and 114 deletions.
2 changes: 1 addition & 1 deletion command_attr/src/util.rs
Expand Up @@ -188,7 +188,7 @@ pub fn create_declaration_validations(fun: &mut CommandFun, dec_for: DeclarFor)
));
}

let context: Type = parse_quote!(&mut serenity::client::Context);
let context: Type = parse_quote!(&serenity::client::Context);
let message: Type = parse_quote!(&serenity::model::channel::Message);
let args: Type = parse_quote!(serenity::framework::standard::Args);
let args2: Type = parse_quote!(&mut serenity::framework::standard::Args);
Expand Down
38 changes: 19 additions & 19 deletions examples/05_command_framework/src/main.rs
Expand Up @@ -114,7 +114,7 @@ If you want more information about a specific command, just pass the command as
// If the `String` is not empty, your given `String` will be used instead.
// If you pass in a `None`, no hint will be displayed at all.
fn my_help(
context: &mut Context,
context: &Context,
msg: &Message,
args: Args,
help_options: &'static HelpOptions,
Expand Down Expand Up @@ -251,7 +251,7 @@ fn main() {
// Options are passed via subsequent attributes.
// Make this command use the "complicated" bucket.
#[bucket = "complicated"]
fn commands(ctx: &mut Context, msg: &Message) -> CommandResult {
fn commands(ctx: &Context, msg: &Message) -> CommandResult {
let mut contents = "Commands used:\n".to_string();

let data = ctx.data.read();
Expand All @@ -272,7 +272,7 @@ fn commands(ctx: &mut Context, msg: &Message) -> CommandResult {
// mentions are replaced with a safe textual alternative.
// In this example channel mentions are excluded via the `ContentSafeOptions`.
#[command]
fn say(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
fn say(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let settings = if let Some(guild_id) = msg.guild_id {
// By default roles, users, and channel mentions are cleaned.
ContentSafeOptions::default()
Expand Down Expand Up @@ -304,7 +304,7 @@ fn say(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
// not called.
#[check]
#[name = "Owner"]
fn owner_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> CheckResult {
fn owner_check(_: &Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> CheckResult {
// Replace 7 with your ID to make this check pass.
//
// `true` will convert into `CheckResult::Success`,
Expand Down Expand Up @@ -332,7 +332,7 @@ fn owner_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions)
#[check_in_help(true)]
// Whether the check shall be displayed in the help-system.
#[display_in_help(true)]
fn admin_check(ctx: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> CheckResult {
fn admin_check(ctx: &Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> CheckResult {
if let Some(member) = msg.member(&ctx.cache) {

if let Ok(permissions) = member.permissions(&ctx.cache) {
Expand All @@ -344,7 +344,7 @@ fn admin_check(ctx: &mut Context, msg: &Message, _: &mut Args, _: &CommandOption
}

#[command]
fn some_long_command(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
fn some_long_command(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, &format!("Arguments: {:?}", args.rest())) {
println!("Error sending message: {:?}", why);
}
Expand All @@ -355,7 +355,7 @@ fn some_long_command(ctx: &mut Context, msg: &Message, args: Args) -> CommandRes
#[command]
// Limits the usage of this command to roles named:
#[allowed_roles("mods", "ultimate neko")]
fn about_role(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
fn about_role(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let potential_role_name = args.rest();

if let Some(guild) = msg.guild(&ctx.cache) {
Expand All @@ -380,7 +380,7 @@ fn about_role(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
#[command]
// Lets us also call `~math *` instead of just `~math multiply`.
#[aliases("*")]
fn multiply(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
fn multiply(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let first = args.single::<f64>()?;
let second = args.single::<f64>()?;

Expand All @@ -394,7 +394,7 @@ fn multiply(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
}

#[command]
fn about(ctx: &mut Context, msg: &Message) -> CommandResult {
fn about(ctx: &Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, "This is a small test-bot! : )") {
println!("Error sending message: {:?}", why);
}
Expand All @@ -403,15 +403,15 @@ fn about(ctx: &mut Context, msg: &Message) -> CommandResult {
}

#[command]
fn latency(ctx: &mut Context, msg: &Message) -> CommandResult {
fn latency(ctx: &Context, msg: &Message) -> CommandResult {
// The shard manager is an interface for mutating, stopping, restarting, and
// retrieving information about shards.
let data = ctx.data.read();

let shard_manager = match data.get::<ShardManagerContainer>() {
Some(v) => v,
None => {
let _ = msg.reply(&ctx, "There was a problem getting the shard manager");
let _ = msg.reply(ctx, "There was a problem getting the shard manager");

return Ok(());
},
Expand All @@ -426,13 +426,13 @@ fn latency(ctx: &mut Context, msg: &Message) -> CommandResult {
let runner = match runners.get(&ShardId(ctx.shard_id)) {
Some(runner) => runner,
None => {
let _ = msg.reply(&ctx, "No shard found");
let _ = msg.reply(ctx, "No shard found");

return Ok(());
},
};

let _ = msg.reply(&ctx, &format!("The shard latency is {:?}", runner.latency));
let _ = msg.reply(ctx, &format!("The shard latency is {:?}", runner.latency));

Ok(())
}
Expand All @@ -441,7 +441,7 @@ fn latency(ctx: &mut Context, msg: &Message) -> CommandResult {
// Limit command usage to guilds.
#[only_in(guilds)]
#[checks(Owner)]
fn ping(ctx: &mut Context, msg: &Message) -> CommandResult {
fn ping(ctx: &Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong! : )") {
println!("Error sending message: {:?}", why);
}
Expand All @@ -456,7 +456,7 @@ fn ping(ctx: &mut Context, msg: &Message) -> CommandResult {
#[bucket = "emoji"]
// Allow only administrators to call this:
#[required_permissions("ADMINISTRATOR")]
fn cat(ctx: &mut Context, msg: &Message) -> CommandResult {
fn cat(ctx: &Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, ":cat:") {
println!("Error sending message: {:?}", why);
}
Expand All @@ -467,7 +467,7 @@ fn cat(ctx: &mut Context, msg: &Message) -> CommandResult {
#[command]
#[description = "Sends an emoji with a dog."]
#[bucket = "emoji"]
fn dog(ctx: &mut Context, msg: &Message) -> CommandResult {
fn dog(ctx: &Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, ":dog:") {
println!("Error sending message: {:?}", why);
}
Expand All @@ -476,7 +476,7 @@ fn dog(ctx: &mut Context, msg: &Message) -> CommandResult {
}

#[command]
fn bird(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
fn bird(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let say_content = if args.is_empty() {
":bird: can find animals for you.".to_string()
} else {
Expand All @@ -491,7 +491,7 @@ fn bird(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
}

#[command]
fn am_i_admin(ctx: &mut Context, msg: &Message) -> CommandResult {
fn am_i_admin(ctx: &Context, msg: &Message) -> CommandResult {
if let Err(why) = msg.channel_id.say(&ctx.http, "Yes you are.") {
println!("Error sending message: {:?}", why);
}
Expand All @@ -500,7 +500,7 @@ fn am_i_admin(ctx: &mut Context, msg: &Message) -> CommandResult {
}

#[command]
fn slow_mode(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
fn slow_mode(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let say_content = if let Ok(slow_mode_rate_seconds) = args.single::<u64>() {
if let Err(why) = msg.channel_id.edit(&ctx.http, |c| c.slow_mode_rate(slow_mode_rate_seconds)) {
println!("Error setting channel's slow mode rate: {:?}", why);
Expand Down
24 changes: 12 additions & 12 deletions examples/06_voice/src/main.rs
Expand Up @@ -78,7 +78,7 @@ fn main() {
}

#[command]
fn deafen(ctx: &mut Context, msg: &Message) -> CommandResult {
fn deafen(ctx: &Context, msg: &Message) -> CommandResult {
let guild_id = match ctx.cache.read().guild_channel(msg.channel_id) {
Some(channel) => channel.read().guild_id,
None => {
Expand All @@ -94,7 +94,7 @@ fn deafen(ctx: &mut Context, msg: &Message) -> CommandResult {
let handler = match manager.get_mut(guild_id) {
Some(handler) => handler,
None => {
check_msg(msg.reply(&ctx, "Not in a voice channel"));
check_msg(msg.reply(ctx, "Not in a voice channel"));

return Ok(());
},
Expand All @@ -112,7 +112,7 @@ fn deafen(ctx: &mut Context, msg: &Message) -> CommandResult {
}

#[command]
fn join(ctx: &mut Context, msg: &Message) -> CommandResult {
fn join(ctx: &Context, msg: &Message) -> CommandResult {
let guild = match msg.guild(&ctx.cache) {
Some(guild) => guild,
None => {
Expand All @@ -133,7 +133,7 @@ fn join(ctx: &mut Context, msg: &Message) -> CommandResult {
let connect_to = match channel_id {
Some(channel) => channel,
None => {
check_msg(msg.reply(&ctx, "Not in a voice channel"));
check_msg(msg.reply(ctx, "Not in a voice channel"));

return Ok(());
}
Expand All @@ -152,7 +152,7 @@ fn join(ctx: &mut Context, msg: &Message) -> CommandResult {
}

#[command]
fn leave(ctx: &mut Context, msg: &Message) -> CommandResult {
fn leave(ctx: &Context, msg: &Message) -> CommandResult {
let guild_id = match ctx.cache.read().guild_channel(msg.channel_id) {
Some(channel) => channel.read().guild_id,
None => {
Expand All @@ -171,14 +171,14 @@ fn leave(ctx: &mut Context, msg: &Message) -> CommandResult {

check_msg(msg.channel_id.say(&ctx.http, "Left voice channel"));
} else {
check_msg(msg.reply(&ctx, "Not in a voice channel"));
check_msg(msg.reply(ctx, "Not in a voice channel"));
}

Ok(())
}

#[command]
fn mute(ctx: &mut Context, msg: &Message) -> CommandResult {
fn mute(ctx: &Context, msg: &Message) -> CommandResult {
let guild_id = match ctx.cache.read().guild_channel(msg.channel_id) {
Some(channel) => channel.read().guild_id,
None => {
Expand All @@ -194,7 +194,7 @@ fn mute(ctx: &mut Context, msg: &Message) -> CommandResult {
let handler = match manager.get_mut(guild_id) {
Some(handler) => handler,
None => {
check_msg(msg.reply(&ctx, "Not in a voice channel"));
check_msg(msg.reply(ctx, "Not in a voice channel"));

return Ok(());
},
Expand All @@ -212,14 +212,14 @@ fn mute(ctx: &mut Context, msg: &Message) -> CommandResult {
}

#[command]
fn ping(context: &mut Context, msg: &Message) -> CommandResult {
fn ping(context: &Context, msg: &Message) -> CommandResult {
check_msg(msg.channel_id.say(&context.http, "Pong!"));

Ok(())
}

#[command]
fn play(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let url = match args.single::<String>() {
Ok(url) => url,
Err(_) => {
Expand Down Expand Up @@ -270,7 +270,7 @@ fn play(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
}

#[command]
fn undeafen(ctx: &mut Context, msg: &Message) -> CommandResult {
fn undeafen(ctx: &Context, msg: &Message) -> CommandResult {
let guild_id = match ctx.cache.read().guild_channel(msg.channel_id) {
Some(channel) => channel.read().guild_id,
None => {
Expand All @@ -295,7 +295,7 @@ fn undeafen(ctx: &mut Context, msg: &Message) -> CommandResult {
}

#[command]
fn unmute(ctx: &mut Context, msg: &Message) -> CommandResult {
fn unmute(ctx: &Context, msg: &Message) -> CommandResult {
let guild_id = match ctx.cache.read().guild_channel(msg.channel_id) {
Some(channel) => channel.read().guild_id,
None => {
Expand Down
2 changes: 1 addition & 1 deletion examples/07_sample_bot_structure/src/commands/math.rs
Expand Up @@ -6,7 +6,7 @@ use serenity::framework::standard::{
};

#[command]
pub fn multiply(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
pub fn multiply(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let one = args.single::<f64>().unwrap();
let two = args.single::<f64>().unwrap();

Expand Down
2 changes: 1 addition & 1 deletion examples/07_sample_bot_structure/src/commands/meta.rs
Expand Up @@ -6,7 +6,7 @@ use serenity::framework::standard::{
};

#[command]
fn ping(ctx: &mut Context, msg: &Message) -> CommandResult {
fn ping(ctx: &Context, msg: &Message) -> CommandResult {
let _ = msg.channel_id.say(&ctx.http, "Pong!");

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions examples/07_sample_bot_structure/src/commands/owner.rs
Expand Up @@ -8,18 +8,18 @@ use serenity::framework::standard::{

#[command]
#[owners_only]
fn quit(ctx: &mut Context, msg: &Message) -> CommandResult {
fn quit(ctx: &Context, msg: &Message) -> CommandResult {
let data = ctx.data.read();

if let Some(manager) = data.get::<ShardManagerContainer>() {
manager.lock().shutdown_all();
} else {
let _ = msg.reply(&ctx, "There was a problem getting the shard manager");
let _ = msg.reply(ctx, "There was a problem getting the shard manager");

return Ok(());
}

let _ = msg.reply(&ctx, "Shutting down!");
let _ = msg.reply(ctx, "Shutting down!");

Ok(())
}
10 changes: 5 additions & 5 deletions examples/10_voice_receive/src/main.rs
Expand Up @@ -114,11 +114,11 @@ fn main() {
}

#[command]
fn join(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
fn join(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let connect_to = match args.single::<u64>() {
Ok(id) => ChannelId(id),
Err(_) => {
check_msg(msg.reply(&ctx, "Requires a valid voice channel ID be given"));
check_msg(msg.reply(ctx, "Requires a valid voice channel ID be given"));

return Ok(());
},
Expand Down Expand Up @@ -147,7 +147,7 @@ fn join(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
}

#[command]
fn leave(ctx: &mut Context, msg: &Message) -> CommandResult {
fn leave(ctx: &Context, msg: &Message) -> CommandResult {
let guild_id = match ctx.cache.read().guild_channel(msg.channel_id) {
Some(channel) => channel.read().guild_id,
None => {
Expand All @@ -166,14 +166,14 @@ fn leave(ctx: &mut Context, msg: &Message) -> CommandResult {

check_msg(msg.channel_id.say(&ctx.http,"Left voice channel"));
} else {
check_msg(msg.reply(&ctx, "Not in a voice channel"));
check_msg(msg.reply(ctx, "Not in a voice channel"));
}

Ok(())
}

#[command]
fn ping(ctx: &mut Context, msg: &Message) -> CommandResult {
fn ping(ctx: &Context, msg: &Message) -> CommandResult {
check_msg(msg.channel_id.say(&ctx.http,"Pong!"));

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions examples/12_timing_and_events/src/main.rs
Expand Up @@ -97,7 +97,7 @@ struct RemindMe;

#[help]
fn my_help(
context: &mut Context,
context: &Context,
msg: &Message,
args: Args,
help_options: &'static HelpOptions,
Expand Down Expand Up @@ -188,7 +188,7 @@ fn thanks_for_reacting(http: Arc<Http>, channel: ChannelId) ->

#[command]
#[aliases("add")]
fn set_reminder(context: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
fn set_reminder(context: &Context, msg: &Message, mut args: Args) -> CommandResult {
// It might be smart to set a moderately high minimum value for `time`
// to avoid abuse like tasks that repeat every 100ms, especially since
// channels have send-message rate limits.
Expand Down

0 comments on commit 3e4294b

Please sign in to comment.