Fix breaking changes caused by Poise update

This commit is contained in:
toast 2024-01-12 13:59:46 +11:00
parent beaeaed159
commit 21e869b053
4 changed files with 47 additions and 34 deletions

View File

@ -7,7 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
poise = "0.6.1" poise = "0.6.1"
reqwest = "0.11.23" reqwest = { version = "0.11.23", features = ["json"] }
serde_json = "1.0.111" serde_json = "1.0.111"
serenity = "0.12.0" serenity = "0.12.0"
tempfile = "3.9.0" tempfile = "3.9.0"

View File

@ -3,6 +3,12 @@ use crate::Error;
use reqwest::get; use reqwest::get;
use std::collections::HashMap; use std::collections::HashMap;
use serde_json::Value; use serde_json::Value;
use poise::CreateReply;
use serenity::builder::{
CreateEmbed,
CreateEmbedAuthor,
CreateEmbedFooter
};
/// Retrieve the data from FS-Server /// Retrieve the data from FS-Server
#[poise::command(slash_command)] #[poise::command(slash_command)]
@ -27,7 +33,6 @@ pub async fn data(
let slot_cap = slots["capacity"].as_i64().unwrap(); let slot_cap = slots["capacity"].as_i64().unwrap();
let slot_cur = slots["used"].as_i64().unwrap(); let slot_cur = slots["used"].as_i64().unwrap();
let daytime = server["dayTime"].as_i64().unwrap(); let daytime = server["dayTime"].as_i64().unwrap();
// println!("{}", daytime);
// todo: Add careerSavegame support when passing in DSS url. // todo: Add careerSavegame support when passing in DSS url.
// So I can get the following values for Autosave, Timescale and Slot usage. // So I can get the following values for Autosave, Timescale and Slot usage.
@ -37,25 +42,24 @@ pub async fn data(
let minute = (daytime / 60 / 1000) % 60; let minute = (daytime / 60 / 1000) % 60;
let time = format!("{:02}:{:02}", hour, minute); let time = format!("{:02}:{:02}", hour, minute);
ctx.send(|m| m.embed(|e| e.color(crate::EMBED_COLOR) let embed = CreateEmbed::new().color(crate::EMBED_COLOR);
.author(|a| ctx.send(CreateReply::default()
a.name(format!("{}/{}", slot_cur, slot_cap)) .embed(embed
.title(name)
.description("*Nobody is playing*")
.fields(vec![
("Map", map, true),
("Version", ver, true),
("Time", &time, true),
("Slot usage", "xx/xx", true),
("Autosave", "xx", true),
("Timescale", "0x", true)
])
.author(CreateEmbedAuthor::new(format!("{}/{}", slot_cur, slot_cap)).clone())
.footer(CreateEmbedFooter::new("Last updated").clone())
.timestamp(poise::serenity_prelude::Timestamp::now())
) )
.title(name) ).await?;
.description("*Nobody is playing*")
.fields(vec![
("Map", map, true),
("Version", ver, true),
("Time", &time, true),
("Slot usage", "xx/xx", true),
("Autosave", "xx", true),
("Timescale", "0x", true)
])
.footer(|f|
f.text("Last updated")
)
.timestamp(poise::serenity_prelude::Timestamp::now())
)).await?;
Ok(()) Ok(())
} }

View File

@ -8,7 +8,7 @@ use std::{
}; };
const WHITELISTED_USERS: &[UserId] = &[ const WHITELISTED_USERS: &[UserId] = &[
UserId(190407856527376384) UserId::new(190407856527376384)
]; ];
/// Evaluate a piece of code /// Evaluate a piece of code

View File

@ -1,7 +1,16 @@
mod commands; mod commands;
use poise::serenity_prelude::{self as serenity};
use std::env::var; use std::env::var;
use poise::serenity_prelude::{self as serenity};
use serenity::{
builder::{
CreateMessage,
CreateEmbed,
CreateEmbedAuthor
},
ClientBuilder,
GatewayIntents
};
type Error = Box<dyn std::error::Error + Send + Sync>; type Error = Box<dyn std::error::Error + Send + Sync>;
@ -15,20 +24,19 @@ async fn on_ready(
) -> Result<(), Error> { ) -> Result<(), Error> {
println!("Connected to API as {}", ready.user.name); println!("Connected to API as {}", ready.user.name);
serenity::ChannelId(BOT_READY_NOTIFY).send_message(&ctx.http, |m| m.embed(|e| let message = CreateMessage::new();
e.color(EMBED_COLOR) let ready_embed = CreateEmbed::new()
.thumbnail(ready.user.avatar_url().unwrap_or_default()) .color(EMBED_COLOR)
.author(|a| a.name(format!("{} is ready!", ready.user.name))) .thumbnail(ready.user.avatar_url().unwrap_or_default())
)).await?; .author(CreateEmbedAuthor::new(format!("{} is ready!", ready.user.name)).clone());
serenity::ChannelId::new(BOT_READY_NOTIFY).send_message(&ctx.http, message.add_embed(ready_embed)).await?;
let register_commands = var("REGISTER_CMDS").unwrap_or_else(|_| String::from("true")).parse::<bool>().unwrap_or(true); let register_commands = var("REGISTER_CMDS").unwrap_or_else(|_| String::from("true")).parse::<bool>().unwrap_or(true);
if register_commands { if register_commands {
let builder = poise::builtins::create_application_commands(&framework.options().commands); let builder = poise::builtins::create_application_commands(&framework.options().commands);
let commands = serenity::Command::set_global_application_commands(&ctx.http, |commands| { let commands = serenity::Command::set_global_commands(&ctx.http, builder).await;
*commands = builder.clone();
commands
}).await;
match commands { match commands {
Ok(cmdmap) => for command in cmdmap.iter() { Ok(cmdmap) => for command in cmdmap.iter() {
@ -45,8 +53,7 @@ async fn on_ready(
async fn main() { async fn main() {
let token = var("DISCORD_TOKEN").expect("Expected a \"DISCORD_TOKEN\" in the envvar but none was found"); let token = var("DISCORD_TOKEN").expect("Expected a \"DISCORD_TOKEN\" in the envvar but none was found");
let client = poise::Framework::builder().token(token) let framework = poise::Framework::builder()
.intents(serenity::GatewayIntents::MESSAGE_CONTENT | serenity::GatewayIntents::GUILDS)
.options(poise::FrameworkOptions { .options(poise::FrameworkOptions {
commands: vec![ commands: vec![
commands::ping::ping(), commands::ping::ping(),
@ -64,7 +71,9 @@ async fn main() {
..Default::default() ..Default::default()
}) })
.setup(|ctx, ready, framework| Box::pin(on_ready(ctx, ready, framework))) .setup(|ctx, ready, framework| Box::pin(on_ready(ctx, ready, framework)))
.build().await.expect("Error while building the client"); .build();
let mut client = ClientBuilder::new(token, GatewayIntents::GUILDS).framework(framework).await.expect("Error creating client");
if let Err(why) = client.start().await { if let Err(why) = client.start().await {
println!("Client error: {:?}", why); println!("Client error: {:?}", why);