Basic Serenity Config
This commit is contained in:
96
src/main.rs
96
src/main.rs
@@ -1,3 +1,95 @@
|
||||
fn main() {
|
||||
println!("Hello, hydra!");
|
||||
/* TODO: Use tracing for better debugging following events */
|
||||
|
||||
use std::env;
|
||||
|
||||
use serenity::async_trait;
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::model::gateway::Ready;
|
||||
use serenity::prelude::*;
|
||||
use serenity::framework::standard::macros::{command, group};
|
||||
use serenity::framework::standard::{CommandResult, StandardFramework};
|
||||
|
||||
#[command]
|
||||
async fn about(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
msg.channel_id.say(&ctx.http, "A simple test bot").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[command]
|
||||
async fn ping(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
msg.channel_id.say(&ctx.http, "pong!").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[group]
|
||||
#[commands(about, ping)]
|
||||
struct General;
|
||||
|
||||
struct Handler;
|
||||
|
||||
#[async_trait]
|
||||
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.
|
||||
async 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!").await {
|
||||
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.
|
||||
async fn ready(&self, _: Context, ready: Ready) {
|
||||
println!("{} is connected!", ready.user.name);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Configure the client with your Discord bot token in the environment.
|
||||
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
||||
// Set gateway intents, which decides what events the bot will be notified about
|
||||
let intents = GatewayIntents::GUILD_MESSAGES
|
||||
| GatewayIntents::GUILD_VOICE_STATES
|
||||
| GatewayIntents::GUILD_MESSAGE_REACTIONS
|
||||
| GatewayIntents::GUILD_MESSAGE_TYPING
|
||||
| GatewayIntents::DIRECT_MESSAGES
|
||||
| GatewayIntents::DIRECT_MESSAGE_REACTIONS
|
||||
| GatewayIntents::DIRECT_MESSAGE_TYPING
|
||||
| GatewayIntents::MESSAGE_CONTENT;
|
||||
|
||||
let framework = StandardFramework::new()
|
||||
.configure(|c| c.prefix("~"))
|
||||
// The `#[group]` (and similarly, `#[command]`) macro generates static instances
|
||||
// containing any options you gave it. For instance, the group `name` and its `commands`.
|
||||
// Their identifiers, names you can use to refer to these instances in code, are an
|
||||
// all-uppercased version of the `name` with a `_GROUP` suffix appended at the end.
|
||||
.group(&GENERAL_GROUP);
|
||||
|
||||
let mut client =
|
||||
Client::builder(&token, intents)
|
||||
.event_handler(Handler)
|
||||
.framework(framework)
|
||||
.await.expect("Err creating client");
|
||||
|
||||
if let Err(why) = client.start().await {
|
||||
println!("Client error: {:?}", why);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user