94 lines
2.7 KiB
Rust
94 lines
2.7 KiB
Rust
use serenity::client::Client;
|
|
use serenity::framework::standard::{
|
|
StandardFramework,
|
|
CommandResult,
|
|
macros::{
|
|
command,
|
|
group
|
|
}
|
|
};
|
|
|
|
group!({
|
|
name: "general",
|
|
options: {},
|
|
commands: [hello, add],
|
|
});
|
|
|
|
use std::env;
|
|
|
|
use serenity::{
|
|
model::{channel::Message, gateway::Ready},
|
|
prelude::*,
|
|
};
|
|
|
|
struct Handler;
|
|
|
|
impl EventHandler for Handler {
|
|
// Set a handler for the `message` event - so that whenever a new message
|
|
// is received - the closure (or function) passed will be called.
|
|
//
|
|
// Event handlers are dispatched through a threadpool, and so multiple
|
|
// events can be dispatched simultaneously.
|
|
fn message(&self, ctx: Context, msg: Message) {
|
|
if msg.content == "!ping" {
|
|
// Sending a message can fail, due to a network error, an
|
|
// authentication error, or lack of permissions to post in the
|
|
// channel, so log to stdout when some error happens, with a
|
|
// description of it.
|
|
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!") {
|
|
println!("Error sending message: {:?}", why);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Set a handler to be called on the `ready` event. This is called when a
|
|
// shard is booted, and a READY payload is sent by Discord. This payload
|
|
// contains data like the current user's guild Ids, current user data,
|
|
// private channels, and more.
|
|
//
|
|
// In this case, just print what the current user's username is.
|
|
fn ready(&self, _: Context, ready: Ready) {
|
|
println!("{} is connected!", ready.user.name);
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
// get token from args
|
|
let args: Vec<_> = env::args().collect();
|
|
let token = &args[1];
|
|
let mut client = Client::new(token, Handler)
|
|
.expect("Error creating client");
|
|
client.with_framework(StandardFramework::new()
|
|
.configure(|c| c.prefix("fishy ")) // set the bot's prefix
|
|
.group(&GENERAL_GROUP));
|
|
|
|
println!("Starting client...");
|
|
// start listening for events by starting a single shard
|
|
if let Err(why) = client.start() {
|
|
println!("An error occurred while running the client: {:?}", why);
|
|
}
|
|
}
|
|
|
|
#[command]
|
|
fn hello(ctx: &mut Context, msg: &Message) -> CommandResult {
|
|
println!("Executing command HELLO as \"{}\"", msg.content);
|
|
msg.reply(ctx, "Hello!")?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[command]
|
|
fn add(ctx: &mut Context, msg: &Message) -> CommandResult {
|
|
println!("Executing command ADD as \"{}\"", msg.content);
|
|
let split: Vec<&str> = msg.content.split(" ").collect();
|
|
if split.len() < 3 {
|
|
msg.reply(ctx, "Please add a message!")?;
|
|
return Ok(());
|
|
}
|
|
|
|
let addition = &split[2..];
|
|
msg.reply(ctx, format!("{}", addition.join(" ")))?;
|
|
|
|
Ok(())
|
|
}
|