mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-18 16:28:55 +00:00
Implement announcer management commands
This commit is contained in:
parent
6879598dfc
commit
a448438c96
3 changed files with 115 additions and 4 deletions
|
@ -1,13 +1,17 @@
|
|||
use crate::AppData;
|
||||
use crate::{AppData, GetCloned};
|
||||
use rayon::prelude::*;
|
||||
use serenity::{
|
||||
framework::standard::{macros::command, Args, CommandError as Error, CommandResult},
|
||||
framework::standard::{
|
||||
macros::{command, group},
|
||||
Args, CommandError as Error, CommandResult,
|
||||
},
|
||||
http::CacheHttp,
|
||||
model::{
|
||||
channel::Message,
|
||||
id::{ChannelId, GuildId, UserId},
|
||||
},
|
||||
prelude::*,
|
||||
utils::MessageBuilder,
|
||||
CacheAndHttp,
|
||||
};
|
||||
use std::{
|
||||
|
@ -162,6 +166,87 @@ impl AnnouncerHandler {
|
|||
#[command("register")]
|
||||
#[description = "Register the current channel with an announcer"]
|
||||
#[usage = "[announcer key]"]
|
||||
#[required_permissions(MANAGE_CHANNELS)]
|
||||
#[only_in(guilds)]
|
||||
#[num_args(1)]
|
||||
pub fn register_announcer(ctx: &mut Context, m: &Message, mut args: Args) -> CommandResult {
|
||||
unimplemented!()
|
||||
let key = args.single::<String>()?;
|
||||
let keys = ctx.data.get_cloned::<AnnouncerHandler>();
|
||||
if !keys.contains(&key.as_str()) {
|
||||
m.reply(
|
||||
&ctx,
|
||||
format!(
|
||||
"Key not found. Available announcer keys are: `{}`",
|
||||
keys.join(", ")
|
||||
),
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
let guild = m.guild(&ctx).expect("Guild-only command");
|
||||
let guild = guild.read();
|
||||
let channel = m.channel_id.to_channel(&ctx)?;
|
||||
AnnouncerChannels::open(&*ctx.data.read())
|
||||
.borrow_mut()?
|
||||
.entry(key.clone())
|
||||
.or_default()
|
||||
.insert(guild.id, m.channel_id);
|
||||
m.reply(
|
||||
&ctx,
|
||||
MessageBuilder::new()
|
||||
.push("Announcer ")
|
||||
.push_mono_safe(key)
|
||||
.push(" has been activated for server ")
|
||||
.push_bold_safe(&guild.name)
|
||||
.push(" on channel ")
|
||||
.push_bold_safe(channel)
|
||||
.build(),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[command("remove")]
|
||||
#[description = "Remove an announcer from the server"]
|
||||
#[usage = "[announcer key]"]
|
||||
#[required_permissions(MANAGE_CHANNELS)]
|
||||
#[only_in(guilds)]
|
||||
#[num_args(1)]
|
||||
pub fn remove_announcer(ctx: &mut Context, m: &Message, mut args: Args) -> CommandResult {
|
||||
let key = args.single::<String>()?;
|
||||
let keys = ctx.data.get_cloned::<AnnouncerHandler>();
|
||||
if !keys.contains(&key.as_str()) {
|
||||
m.reply(
|
||||
&ctx,
|
||||
format!(
|
||||
"Key not found. Available announcer keys are: `{}`",
|
||||
keys.join(", ")
|
||||
),
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
let guild = m.guild(&ctx).expect("Guild-only command");
|
||||
let guild = guild.read();
|
||||
AnnouncerChannels::open(&*ctx.data.read())
|
||||
.borrow_mut()?
|
||||
.entry(key.clone())
|
||||
.and_modify(|m| {
|
||||
m.remove(&guild.id);
|
||||
});
|
||||
m.reply(
|
||||
&ctx,
|
||||
MessageBuilder::new()
|
||||
.push("Announcer ")
|
||||
.push_mono_safe(key)
|
||||
.push(" has been de-activated for server ")
|
||||
.push_bold_safe(&guild.name)
|
||||
.build(),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[group("announcer")]
|
||||
#[prefix("announcer")]
|
||||
#[only_in(guilds)]
|
||||
#[required_permissions(MANAGE_CHANNELS)]
|
||||
#[description = "Manage the announcers in the server."]
|
||||
#[commands(remove_announcer, register_announcer)]
|
||||
pub struct AnnouncerCommands;
|
||||
|
|
|
@ -50,3 +50,28 @@ impl GetCloned for AppData {
|
|||
self.read().get::<T>().cloned().expect("Should be there")
|
||||
}
|
||||
}
|
||||
|
||||
pub mod prelude_commands {
|
||||
use crate::announcer::ANNOUNCERCOMMANDS_GROUP;
|
||||
use serenity::{
|
||||
framework::standard::{
|
||||
macros::{command, group},
|
||||
CommandResult,
|
||||
},
|
||||
model::channel::Message,
|
||||
prelude::Context,
|
||||
};
|
||||
|
||||
#[group("Prelude")]
|
||||
#[description = "All the commands that makes the base of Youmu"]
|
||||
#[commands(ping)]
|
||||
#[sub_groups(AnnouncerCommands)]
|
||||
pub struct Prelude;
|
||||
|
||||
#[command]
|
||||
#[description = "pong!"]
|
||||
fn ping(ctx: &mut Context, m: &Message) -> CommandResult {
|
||||
m.reply(&ctx, "Pong!")?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -165,7 +165,8 @@ fn setup_framework(client: &Client) -> StandardFramework {
|
|||
.bucket("images", |c| c.time_span(60).limit(2))
|
||||
.bucket("community", |c| {
|
||||
c.delay(30).time_span(30).limit(1)
|
||||
});
|
||||
})
|
||||
.group(&prelude_commands::PRELUDE_GROUP);
|
||||
// groups here
|
||||
#[cfg(feature = "core")]
|
||||
let fw = fw
|
||||
|
|
Loading…
Add table
Reference in a new issue