Skip to content

Instantly share code, notes, and snippets.

@AregevDev
Created April 25, 2019 10:26
Show Gist options
  • Save AregevDev/0491f2dbff44e427080a7a22d06aa7fd to your computer and use it in GitHub Desktop.
Save AregevDev/0491f2dbff44e427080a7a22d06aa7fd to your computer and use it in GitHub Desktop.
Discord bot in Rust WIP
use serenity::model::prelude::{Channel, UserId};
use serenity::utils::Colour;
use serenity::{
framework::{
standard::macros::{check, command, group, help},
standard::{
help_commands, Args, CheckResult, CommandGroup, CommandOptions, CommandResult,
HelpOptions,
},
StandardFramework,
},
model::{channel::Message, gateway::Ready},
prelude::*,
};
use std::collections::HashSet;
use std::hash::BuildHasher;
struct Handler;
static X: i32 = 0xffffff;
impl EventHandler for Handler {
fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
group!({
name: "general",
options: {},
commands: [ping]
});
group!({
name: "owner",
options: {
checks: [Admin]
},
commands: [slow],
});
#[check]
#[name = "Admin"]
fn is_admin(ctx: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> CheckResult {
if let Some(member) = msg.member(&ctx.cache) {
if let Ok(permissions) = member.permissions(&ctx.cache) {
return permissions.administrator().into();
}
}
false.into()
}
#[command]
fn ping(ctx: &mut Context, message: &Message) -> CommandResult {
message.channel_id.say(&ctx, "UwU")?;
Ok(())
}
#[command]
fn slow(ctx: &mut Context, message: &Message, mut args: Args) -> CommandResult {
let say_content = if let Ok(slow_mode_rate_seconds) = args.single::<u64>() {
if let Err(why) = message
.channel_id
.edit(&ctx.http, |c| c.slow_mode_rate(slow_mode_rate_seconds))
{
println!("Error setting channel's slow mode rate: {:?}", why);
format!(
"Failed to set slow mode to `{}` seconds.",
slow_mode_rate_seconds
)
} else {
format!(
"Successfully set slow mode rate to `{}` seconds.",
slow_mode_rate_seconds
)
}
} else if let Some(Channel::Guild(channel)) = message.channel_id.to_channel_cached(&ctx.cache) {
format!(
"Current slow mode rate is `{}` seconds.",
channel.read().slow_mode_rate.unwrap_or(0)
)
} else {
"Failed to find channel in cache.".to_string()
};
if let Err(why) = message.channel_id.say(&ctx.http, say_content) {
println!("Error sending message: {:?}", why);
}
Ok(())
}
#[help]
fn help(
context: &mut Context,
msg: &Message,
args: Args,
help_options: &'static HelpOptions,
groups: &[&'static CommandGroup],
owners: HashSet<UserId, impl BuildHasher>,
) -> CommandResult {
let mut clone = &mut help_options.clone();
clone.embed_success_colour = Colour::from(0xffffff);
msg.channel_id.broadcast_typing(&context)?;
help_commands::with_embeds(context, msg, args, clone, groups, owners)
}
fn main() {
let token = "NTQ5MzA4MDc5NjQ5MzI1Mjcw.XMCa1w.TvUxSpKyXc1s4fmubgOS8_FLOqQ";
let mut client = Client::new(&token, Handler).expect("Err creating client");
client.with_framework(
StandardFramework::new()
.configure(|c| c.prefix("~"))
.before(|ctx, message, command_name| {
println!(
"Got command '{}' by user '{}'",
command_name, message.author.id
);
true
})
.group(&GENERAL_GROUP)
.group(&OWNER_GROUP)
.help(&HELP_HELP_COMMAND),
);
if let Err(why) = client.start() {
println!("Client error: {:?}", why);
}
}
@MikeTheSapien
Copy link

I recommend not posting your token here? Unless it's not a real token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment