Skip to content

Commit

Permalink
changed code to work with new serinity version
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwai committed Feb 11, 2024
1 parent 695fb6d commit 5ed0508
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 278 deletions.
10 changes: 5 additions & 5 deletions src/bot/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ pub async fn is_admin(ctx: Context<'_>) -> bool {

/// Returns false in case of an error
pub async fn is_bot_admin(ctx: Context<'_>) -> bool {
let author_id = ctx.author().id.0;
let author_id = ctx.author().id;
let main_guild_id = env::MAIN_GUILD_ID.get().unwrap();

let main_guild_member = ctx.http().get_member(*main_guild_id, author_id).await
let main_guild_member = ctx.http().get_member(GuildId::new(*main_guild_id), author_id).await
.map_err(|err| error!(
error = err.to_string(),
member_id = author_id,
member_id = author_id.get(),
"Could not get main guild member"
)).ok();

Expand All @@ -75,7 +75,7 @@ pub async fn is_bot_admin(ctx: Context<'_>) -> bool {
};


let main_guild_roles = GuildId(*main_guild_id).roles(ctx.http()).await
let main_guild_roles = GuildId::new(*main_guild_id).roles(ctx.http()).await
.map_err(|err| error!(error = err.to_string(), "Could not get main guild roles"))
.ok();

Expand Down Expand Up @@ -117,7 +117,7 @@ pub async fn sent_in_main_guild(ctx: Context<'_>) -> bool {
if let Some(guild) = ctx.guild() {
return env::MAIN_GUILD_ID
.get()
.map_or(false, |&main_guild_id| guild.id.0 == main_guild_id);
.map_or(false, |&main_guild_id| guild.id.get() == main_guild_id);
}
false
}
Expand Down
7 changes: 4 additions & 3 deletions src/bot/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
logging, mysql_lib,
};
use std::time::SystemTime;
use poise::CreateReply;

/// ping command
#[poise::command(slash_command, prefix_command)]
Expand All @@ -14,14 +15,14 @@ pub async fn ping(ctx: Context<'_>) -> Result<(), Error> {
let now = SystemTime::now();
let reply_message = ctx.say(response).await?;
reply_message
.edit(ctx, |builder| {
builder.content(match now.elapsed() {
.edit(ctx, CreateReply::default()
.content(match now.elapsed() {
Ok(elapsed) => {
format!("Pong: {} ms", elapsed.as_millis())
}
Err(_) => "Pong: could not calculate time difference".to_owned(),
})
})
)
.await?;
Ok(())
}
Expand Down
50 changes: 22 additions & 28 deletions src/bot/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::Arc;
use std::time::Duration;

use poise::serenity_prelude as serenity;
Expand Down Expand Up @@ -30,33 +31,30 @@ pub async fn entrypoint(database_pool: Pool<MySql>, redis_client: Client) {
let db_clone = database_pool.clone();
let framework = Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![
commands::ping(),
commands::logger_pipe()
],
commands: vec![commands::ping(), commands::logger_pipe()],
allowed_mentions: Some({
let mut f = serenity::CreateAllowedMentions::default();
f.empty_parse()
.parse(serenity::ParseValue::Users)
.replied_user(true);
f
serenity::CreateAllowedMentions::default()
.replied_user(true)
.all_users(true)
}),
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("!".into()),
edit_tracker: Some(poise::EditTracker::for_timespan(Duration::from_secs(3600))),
edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan(
Duration::from_secs(3600),
))),
..Default::default()
},
pre_command: |ctx| {
Box::pin(async move {
info!(
guild_id = ctx.guild_id().map(|id| id.0).unwrap_or(0),
guild_id = ctx.guild_id().map(|id| id.get()).unwrap_or(1),
"Received Command from @{}, in guild {}, in channel #{}: `{}`",
ctx.author().name,
ctx.guild()
.map(|guild| guild.name)
.map(|guild| guild.name.clone())
.unwrap_or("no-guild".to_string()),
ctx.channel_id()
.name(ctx.cache())
.name(ctx)
.await
.unwrap_or("Unknown".to_string()),
ctx.invocation_string(),
Expand All @@ -65,10 +63,6 @@ pub async fn entrypoint(database_pool: Pool<MySql>, redis_client: Client) {
},
..Default::default()
})
.token(env::BOT_TOKEN.get().unwrap())
.intents(
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT,
)
.setup(|_ctx, ready, _framework| {
Box::pin(async move {
info!("Logged in as {}", ready.user.name);
Expand All @@ -77,18 +71,18 @@ pub async fn entrypoint(database_pool: Pool<MySql>, redis_client: Client) {
redis_client,
})
})
});

let built_framework = framework.build().await.expect("Err building poise client");
})
.build();

logging::setup_discord_logging(
built_framework.client().cache_and_http.http.clone(),
db_clone,
let mut client = serenity::Client::builder(
env::BOT_TOKEN.get().unwrap(),
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT,
)
.await;
.framework(framework)
.await
.unwrap();

logging::setup_discord_logging(client.http.clone(), db_clone).await;

built_framework
.start()
.await
.expect("Err running poise client");
client.start().await.expect("Err running poise client");
}
22 changes: 11 additions & 11 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs;
use std::num::NonZeroU64;
use std::sync::{Arc, OnceLock};

use poise::serenity_prelude::{ChannelId, GuildId, Http};
use poise::serenity_prelude::{ChannelId, CreateMessage, GuildId, Http};
use poise::serenity_prelude::futures::executor::block_on;
use rolling_file::RollingConditionBasic;
use sqlx::{MySql, Pool};
Expand Down Expand Up @@ -74,14 +74,14 @@ pub async fn setup_discord_logging(discord_http: Arc<Http>, db: Pool<MySql>) {

// Setup main logging guild/channel
let main_guild_channels = discord_http
.get_channels(*env::MAIN_GUILD_ID.get().unwrap())
.get_channels(GuildId::new(*env::MAIN_GUILD_ID.get().unwrap()))
.await
.expect("Could not get main guild");

let main_logging_channel = main_guild_channels[0].id;

modify_discord_layer(|discord_layer| {
discord_layer.main_log_channel = NonZeroU64::new(main_logging_channel.0);
discord_layer.main_log_channel = NonZeroU64::new(main_logging_channel.get());
});

// Setup logging channels per server
Expand Down Expand Up @@ -172,11 +172,10 @@ where
let local_discord_http = discord_http.clone();
let message_copy = message.clone();
spawn_blocking(move || {
let _ = block_on(
ChannelId(channel_id.get()).send_message(local_discord_http, |m| {
m.content(format!("{event_level} {}", message_copy))
}),
);
let _ = block_on(ChannelId::new(channel_id.get()).send_message(
local_discord_http,
CreateMessage::new().content(format!("{event_level} {}", message_copy)),
));
});
}

Expand All @@ -185,9 +184,10 @@ where
let channel_id = *channel_id;
let local_discord_http = discord_http.clone();
spawn_blocking(move || {
let _ = block_on(channel_id.send_message(local_discord_http.clone(), |m| {
m.content(format!("{event_level} {message}"))
}));
let _ = block_on(channel_id.send_message(
local_discord_http.clone(),
CreateMessage::new().content(format!("{event_level} {message}")),
));
});
}
}
Expand Down
Loading

0 comments on commit 5ed0508

Please sign in to comment.