chore: update bot framework

This commit updates the crate poise to its latest version
This commit is contained in:
2024-01-18 20:47:37 +01:00
parent 58cbde4cef
commit 0dc4e1cd51
7 changed files with 309 additions and 209 deletions

View File

@@ -43,7 +43,7 @@ impl Database {
&self,
guild_id: GuildId,
) -> Result<Vec<ChannelId>> {
let guild_id = guild_id.0 as i64;
let guild_id = guild_id.get() as i64;
sqlx::query!(
r#"
SELECT channel_id
@@ -62,7 +62,7 @@ WHERE guild_id = ?1"#,
.map(|channels| {
channels
.iter()
.map(|id| ChannelId(id.channel_id as u64))
.map(|id| ChannelId::new(id.channel_id as u64))
.collect()
})
}
@@ -80,8 +80,8 @@ WHERE guild_id = ?1"#,
guild_id: GuildId,
channel_id: ChannelId,
) -> Result<()> {
let guild_id = guild_id.0 as i64;
let channel_id = channel_id.0 as i64;
let guild_id = guild_id.get() as i64;
let channel_id = channel_id.get() as i64;
let mut conn = self.0.acquire().await?;
sqlx::query!(r#"
@@ -112,8 +112,8 @@ VALUES ( ?1, ?2 )"#,
guild: GuildId,
channel: ChannelId,
) -> Result<()> {
let guild_id = guild.0 as i64;
let channel_id = channel.0 as i64;
let guild_id = guild.get() as i64;
let channel_id = channel.get() as i64;
let mut conn = self.0.acquire().await?;
sqlx::query!(r#"
DELETE FROM guild_log_channels

View File

@@ -4,19 +4,18 @@ use super::super::Result;
use super::super::error::Error as DiscordError;
use poise::serenity_prelude::{self as serenity, CreateEmbed};
use poise::serenity_prelude::{self as serenity, CreateEmbed, CreateMessage};
use tracing::{error, info};
fn message_for_everyone_mention(
embed: &mut CreateEmbed,
fn create_embed_for_mention(
message: &serenity::Message,
guild_id: u64,
) {
let author = message.author.clone();
let message_channel = message.channel_id.0;
embed
) -> CreateEmbed {
let author_id = message.author.id.to_string();
let message_channel = message.channel_id.get();
CreateEmbed::new()
.title("Someone mentioned everyone!")
.field("Author", author.clone(), true)
.field("Author", format!("<@{author_id}>"), true)
.field("When", message.timestamp.naive_local().to_string(), true)
.field("Channel", format!("<#{message_channel}>"), true)
.field(
@@ -26,7 +25,7 @@ fn message_for_everyone_mention(
message.id
),
false,
);
)
}
/// Handle messages mentioning everyone.
@@ -54,13 +53,10 @@ pub async fn handle_everyone_mention(
database.get_logging_channels(guild_id).await?;
for channel in &channels {
// Ignore result, it'll be in the bot's logger
let embed = create_embed_for_mention(message, guild_id.get());
let builder = CreateMessage::new().embed(embed);
let _ = channel
.send_message(&ctx, |m| {
m.embed(|e| {
message_for_everyone_mention(e, message, guild_id.0);
e
})
})
.send_message(&ctx, builder)
.await
.map_err(|e| error!("Failed to send message: {e:?}"));
}

View File

@@ -1,9 +1,6 @@
use super::{utils::BotData, Error, Result};
use poise::{
serenity_prelude::{self as serenity},
Event,
};
use poise::serenity_prelude::{self as serenity, FullEvent};
use tracing::info;
mod everyone;
@@ -17,15 +14,15 @@ use everyone::handle_everyone_mention;
/// themselves.
pub async fn event_handler(
ctx: &serenity::Context,
event: &Event<'_>,
event: &FullEvent,
_framework: poise::FrameworkContext<'_, BotData, Error>,
data: &BotData,
) -> Result {
match event {
Event::Ready { data_about_bot } => {
FullEvent::Ready { data_about_bot } => {
info!("Logged in as {}", data_about_bot.user.name);
}
Event::Message { new_message } => {
FullEvent::Message { new_message } => {
handle_everyone_mention(ctx, &data.database, new_message).await?;
}
_ => {}

View File

@@ -1,9 +1,9 @@
mod commands;
pub mod error;
mod events;
pub mod utils;
pub mod error;
use poise::FrameworkBuilder;
use poise::serenity_prelude::ClientBuilder;
use utils::serenity;
use commands::logging;
@@ -18,8 +18,10 @@ pub type Result = ::std::result::Result<(), Error>;
/// # Panics
///
/// Panics if the environment `DISCORD_TOKEN` is unavailable.
pub fn make_bot() -> FrameworkBuilder<BotData, Error> {
poise::Framework::builder()
pub fn make_bot() -> ClientBuilder {
let intents = serenity::GatewayIntents::non_privileged();
let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![logging()],
event_handler: |ctx, event, framework, data| {
@@ -27,8 +29,6 @@ pub fn make_bot() -> FrameworkBuilder<BotData, Error> {
},
..Default::default()
})
.token(std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"))
.intents(serenity::GatewayIntents::non_privileged())
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(
@@ -39,4 +39,6 @@ pub fn make_bot() -> FrameworkBuilder<BotData, Error> {
Ok(BotData::new().await?)
})
})
.build();
ClientBuilder::new(token, intents).framework(framework)
}

View File

@@ -11,8 +11,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
utils::setup_logging();
color_eyre::install()?;
let bot = discord::make_bot();
bot.run().await?;
let mut bot = discord::make_bot().await?;
bot.start().await?;
Ok(())
}