Skip to content

Commit

Permalink
Remove multiple event handler support
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Apr 24, 2024
1 parent 92d872a commit 3128cc9
Show file tree
Hide file tree
Showing 29 changed files with 151 additions and 174 deletions.
4 changes: 2 additions & 2 deletions examples/e01_basic_ping_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl EventHandler for Handler {
//
// Event handlers are dispatched through a threadpool, and so multiple events can be dispatched
// simultaneously.
async fn message(&self, ctx: &Context, msg: &Message) {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "!ping" {
// Sending a message can fail, due to a network error, an authentication error, or lack
// of permissions to post in the channel, so log to stdout when some error happens,
Expand All @@ -29,7 +29,7 @@ impl EventHandler for Handler {
// Ids, current user data, private channels, and more.
//
// In this case, just print what the current user's username is.
async fn ready(&self, _: &Context, ready: &Ready) {
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/e02_transparent_guild_sharding/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct Handler;

#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: &Context, msg: &Message) {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "!ping" {
println!("Shard {}", ctx.shard_id);

Expand All @@ -34,7 +34,7 @@ impl EventHandler for Handler {
}
}

async fn ready(&self, _: &Context, ready: &Ready) {
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/e03_struct_utilities/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct Handler;

#[async_trait]
impl EventHandler for Handler {
async fn message(&self, context: &Context, msg: &Message) {
async fn message(&self, context: Context, msg: Message) {
if msg.content == "!messageme" {
// If the `utils`-feature is enabled, then model structs will have a lot of useful
// methods implemented, to avoid using an often otherwise bulky Context, or even much
Expand All @@ -27,7 +27,7 @@ impl EventHandler for Handler {
}
}

async fn ready(&self, _: &Context, ready: &Ready) {
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/e04_message_builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct Handler;

#[async_trait]
impl EventHandler for Handler {
async fn message(&self, context: &Context, msg: &Message) {
async fn message(&self, context: Context, msg: Message) {
if msg.content == "!ping" {
let channel = match msg.channel_id.to_channel(&context).await {
Ok(channel) => channel,
Expand Down Expand Up @@ -38,7 +38,7 @@ impl EventHandler for Handler {
}
}

async fn ready(&self, _: &Context, ready: &Ready) {
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/e05_sample_bot_structure/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Handler;

#[async_trait]
impl EventHandler for Handler {
async fn interaction_create(&self, ctx: &Context, interaction: &Interaction) {
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
if let Interaction::Command(command) = interaction {
println!("Received command interaction: {command:#?}");

Expand All @@ -22,7 +22,7 @@ impl EventHandler for Handler {
"id" => Some(commands::id::run(&command.data.options())),
"attachmentinput" => Some(commands::attachmentinput::run(&command.data.options())),
"modal" => {
commands::modal::run(ctx, command).await.unwrap();
commands::modal::run(&ctx, &command).await.unwrap();
None
},
_ => Some("not implemented :(".to_string()),
Expand All @@ -38,7 +38,7 @@ impl EventHandler for Handler {
}
}

async fn ready(&self, ctx: &Context, ready: &Ready) {
async fn ready(&self, ctx: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);

let guild_id = GuildId::new(
Expand Down
4 changes: 2 additions & 2 deletions examples/e06_env_logging/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct Handler;

#[async_trait]
impl EventHandler for Handler {
async fn ready(&self, _: &Context, ready: &Ready) {
async fn ready(&self, _: Context, ready: Ready) {
// Log at the INFO level. This is a macro from the `tracing` crate.
info!("{} is connected!", ready.user.name);
}
Expand All @@ -20,7 +20,7 @@ impl EventHandler for Handler {
// Handler doesn't implement Debug here, so we specify to skip that argument.
// Context doesn't implement Debug either, so it is also skipped.
#[instrument(skip(self, _ctx))]
async fn resume(&self, _ctx: &Context, _resume: &ResumedEvent) {
async fn resume(&self, _ctx: Context, _resume: ResumedEvent) {
// Log at the DEBUG level.
//
// In this example, this will not show up in the logs because DEBUG is
Expand Down
2 changes: 1 addition & 1 deletion examples/e07_shard_manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct Handler;

#[async_trait]
impl EventHandler for Handler {
async fn ready(&self, _: &Context, ready: &Ready) {
async fn ready(&self, _: Context, ready: Ready) {
if let Some(shard) = ready.shard {
// Note that array index 0 is 0-indexed, while index 1 is 1-indexed.
//
Expand Down
4 changes: 2 additions & 2 deletions examples/e08_create_message_builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct Handler;

#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: &Context, msg: &Message) {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "!hello" {
// The create message builder allows you to easily create embeds and messages using a
// builder syntax.
Expand Down Expand Up @@ -43,7 +43,7 @@ impl EventHandler for Handler {
}
}

async fn ready(&self, _: &Context, ready: &Ready) {
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/e09_collectors/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ struct Handler;

#[async_trait]
impl EventHandler for Handler {
async fn ready(&self, _: &Context, ready: &Ready) {
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}

async fn message(&self, ctx: &Context, msg: &Message) {
async fn message(&self, ctx: Context, msg: Message) {
let mut score = 0u32;
let _ = msg.reply(&ctx, "How was that crusty crab called again? 10 seconds time!").await;

Expand Down
6 changes: 3 additions & 3 deletions examples/e10_gateway_intents/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ struct Handler;
#[async_trait]
impl EventHandler for Handler {
// This event will be dispatched for guilds, but not for direct messages.
async fn message(&self, _ctx: &Context, msg: &Message) {
async fn message(&self, _ctx: Context, msg: Message) {
println!("Received message: {}", msg.content);
}

// As the intents set in this example, this event shall never be dispatched.
// Try it by changing your status.
async fn presence_update(&self, _ctx: &Context, _new_data: &Presence) {
async fn presence_update(&self, _ctx: Context, _new_data: Presence) {
println!("Presence Update");
}

async fn ready(&self, _: &Context, ready: &Ready) {
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/e11_global_data/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct Handler;

#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: &Context, msg: &Message) {
async fn message(&self, ctx: Context, msg: Message) {
// Since data is located in Context, this means you are able to use it within events!
let data = ctx.data::<UserData>();

Expand Down Expand Up @@ -54,7 +54,7 @@ impl EventHandler for Handler {
}
}

async fn ready(&self, _: &Context, ready: &Ready) {
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
Expand Down
9 changes: 4 additions & 5 deletions examples/e12_parallel_loops/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ struct Handler {

#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: &Context, msg: &Message) {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content.starts_with("!ping") {
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
eprintln!("Error sending message: {why:?}");
}
}
}

async fn ready(&self, _ctx: &Context, ready: &Ready) {
async fn ready(&self, _ctx: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}

// We use the cache_ready event just in case some cache operation is required in whatever use
// case you have for this.
async fn cache_ready(&self, ctx: &Context, _guilds: &Vec<GuildId>) {
async fn cache_ready(&self, ctx: Context, _guilds: Vec<GuildId>) {
println!("Cache built successfully!");

// We need to check that the loop is not already running when this event triggers, as this
Expand All @@ -53,10 +53,9 @@ impl EventHandler for Handler {
});

// And of course, we can run more than one thread at different timings.
let ctx2 = ctx.clone();
tokio::spawn(async move {
loop {
set_activity_to_current_time(&ctx2);
set_activity_to_current_time(&ctx);
tokio::time::sleep(Duration::from_secs(60)).await;
}
});
Expand Down
2 changes: 1 addition & 1 deletion examples/e13_sqlite_database/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct Bot {

#[async_trait]
impl EventHandler for Bot {
async fn message(&self, ctx: &Context, msg: &Message) {
async fn message(&self, ctx: Context, msg: Message) {
let user_id = msg.author.id.get() as i64;

if let Some(task_description) = msg.content.strip_prefix("~todo add") {
Expand Down
2 changes: 1 addition & 1 deletion examples/e14_message_components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Handler;

#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: &Context, msg: &Message) {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content != "animal" {
return;
}
Expand Down
14 changes: 7 additions & 7 deletions examples/testing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod model_type_sizes;
const IMAGE_URL: &str = "https://raw.githubusercontent.com/serenity-rs/serenity/current/logo.png";
const IMAGE_URL_2: &str = "https://rustacean.net/assets/rustlogo.png";

async fn message(ctx: &Context, msg: &Message) -> Result<(), serenity::Error> {
async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
let channel_id = msg.channel_id;
let guild_id = msg.guild_id.unwrap();
if let Some(_args) = msg.content.strip_prefix("testmessage ") {
Expand Down Expand Up @@ -235,7 +235,7 @@ async fn message(ctx: &Context, msg: &Message) -> Result<(), serenity::Error> {

async fn interaction(
ctx: &Context,
interaction: &CommandInteraction,
interaction: CommandInteraction,
) -> Result<(), serenity::Error> {
if interaction.data.name == "editattachments" {
// Respond with an image
Expand Down Expand Up @@ -376,13 +376,13 @@ async fn interaction(
struct Handler;
#[serenity::async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: &Context, msg: &Message) {
message(ctx, msg).await.unwrap();
async fn message(&self, ctx: Context, msg: Message) {
message(&ctx, msg).await.unwrap();
}

async fn interaction_create(&self, ctx: &Context, i: &Interaction) {
async fn interaction_create(&self, ctx: Context, i: Interaction) {
match i {
Interaction::Command(i) => interaction(ctx, i).await.unwrap(),
Interaction::Command(i) => interaction(&ctx, i).await.unwrap(),
Interaction::Component(i) => println!("{:#?}", i.data),
Interaction::Autocomplete(i) => {
i.create_response(
Expand All @@ -399,7 +399,7 @@ impl EventHandler for Handler {
}
}

async fn reaction_remove_emoji(&self, _ctx: &Context, removed_reactions: &Reaction) {
async fn reaction_remove_emoji(&self, _ctx: Context, removed_reactions: Reaction) {
println!("Got ReactionRemoveEmoji event: {removed_reactions:?}");
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl Cache {
/// #[serenity::async_trait]
/// # #[cfg(feature = "client")]
/// impl EventHandler for Handler {
/// async fn cache_ready(&self, ctx: &Context, _: &Vec<GuildId>) {
/// async fn cache_ready(&self, ctx: Context, _: Vec<GuildId>) {
/// println!("{} unknown members", ctx.cache.unknown_members());
/// }
/// }
Expand Down Expand Up @@ -307,7 +307,7 @@ impl Cache {
///
/// #[serenity::async_trait]
/// impl EventHandler for Handler {
/// async fn ready(&self, context: &Context, _: &Ready) {
/// async fn ready(&self, context: Context, _: Ready) {
/// let guilds = context.cache.guilds().len();
///
/// println!("Guilds in the Cache: {}", guilds);
Expand Down
16 changes: 8 additions & 8 deletions src/client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Context {
///
/// #[serenity::async_trait]
/// impl EventHandler for Handler {
/// async fn message(&self, ctx: &Context, msg: &Message) {
/// async fn message(&self, ctx: Context, msg: Message) {
/// if msg.content == "!online" {
/// ctx.online();
/// }
Expand Down Expand Up @@ -142,7 +142,7 @@ impl Context {
///
/// #[serenity::async_trait]
/// impl EventHandler for Handler {
/// async fn message(&self, ctx: &Context, msg: &Message) {
/// async fn message(&self, ctx: Context, msg: Message) {
/// if msg.content == "!idle" {
/// ctx.idle();
/// }
Expand Down Expand Up @@ -170,7 +170,7 @@ impl Context {
///
/// #[serenity::async_trait]
/// impl EventHandler for Handler {
/// async fn message(&self, ctx: &Context, msg: &Message) {
/// async fn message(&self, ctx: Context, msg: Message) {
/// if msg.content == "!dnd" {
/// ctx.dnd();
/// }
Expand Down Expand Up @@ -198,7 +198,7 @@ impl Context {
///
/// #[serenity::async_trait]
/// impl EventHandler for Handler {
/// async fn message(&self, ctx: &Context, msg: &Message) {
/// async fn message(&self, ctx: Context, msg: Message) {
/// if msg.content == "!invisible" {
/// ctx.invisible();
/// }
Expand Down Expand Up @@ -229,7 +229,7 @@ impl Context {
///
/// #[serenity::async_trait]
/// impl EventHandler for Handler {
/// async fn message(&self, ctx: &Context, msg: &Message) {
/// async fn message(&self, ctx: Context, msg: Message) {
/// if msg.content == "!reset_presence" {
/// ctx.reset_presence();
/// }
Expand Down Expand Up @@ -259,7 +259,7 @@ impl Context {
///
/// #[serenity::async_trait]
/// impl EventHandler for Handler {
/// async fn message(&self, ctx: &Context, msg: &Message) {
/// async fn message(&self, ctx: Context, msg: Message) {
/// let mut args = msg.content.splitn(2, ' ');
///
/// if let (Some("~setgame"), Some(game)) = (args.next(), args.next()) {
Expand All @@ -286,7 +286,7 @@ impl Context {
///
/// #[serenity::async_trait]
/// impl EventHandler for Handler {
/// async fn ready(&self, ctx: &Context, _: &Ready) {
/// async fn ready(&self, ctx: Context, _: Ready) {
/// use serenity::model::user::OnlineStatus;
///
/// ctx.set_presence(None, OnlineStatus::Idle);
Expand All @@ -303,7 +303,7 @@ impl Context {
///
/// #[serenity::async_trait]
/// impl EventHandler for Handler {
/// async fn ready(&self, context: &Context, _: &Ready) {
/// async fn ready(&self, context: Context, _: Ready) {
/// use serenity::gateway::ActivityData;
/// use serenity::model::user::OnlineStatus;
///
Expand Down
Loading

0 comments on commit 3128cc9

Please sign in to comment.