Skip to content

Instantly share code, notes, and snippets.

@Erk-
Created February 16, 2019 12:19
Show Gist options
  • Save Erk-/05a82137a81e1ff94028effedf8f6b28 to your computer and use it in GitHub Desktop.
Save Erk-/05a82137a81e1ff94028effedf8f6b28 to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate serenity;
#[macro_use]
extern crate structopt;
use structopt::StructOpt;
use std::{collections::HashMap, env, fmt::Write, sync::Arc};
use serenity::{
client::bridge::gateway::{ShardId, ShardManager},
framework::standard::{
help_commands, Args, CommandOptions, DispatchError, HelpBehaviour, StandardFramework,
},
model::{channel::Message, gateway::Ready, Permissions},
prelude::*,
utils::{content_safe, ContentSafeOptions},
};
use serenity::framework::standard::Command;
use serenity::framework::standard::CommandError;
// This imports `typemap`'s `Key` as `TypeMapKey`.
use serenity::prelude::*;
struct Handler;
impl EventHandler for Handler {
fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
}
}
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",
);
let mut client = Client::new(&token, Handler).expect("Err creating client");
client.with_framework(
StandardFramework::new()
.configure(|c| c
.allow_whitespace(true)
.on_mention(true)
.prefix(".")
.delimiters(vec![", ", ","]))
.command("ping", |c| c
.cmd(ping))
);
if let Err(why) = client.start() {
println!("Client error: {:?}", why);
}
}
#[derive(StructOpt, PartialEq, Debug)]
struct Opt {
#[structopt(short = "a", long = "arg")]
arg: i32,
}
struct ping;
impl Command for ping {
fn execute(&self, ctx: &mut Context, msg: &Message, args: Args) -> Result<(), CommandError> {
let original = msg.content.split_whitespace();
let options = Opt::from_iter_safe(original).unwrap_or(Opt { arg : 0 }); // Error handling here
if let Err(why) = msg.channel_id.say(format!("Pong! : ) ({})", options.arg)) {
println!("Error sending message: {:?}", why);
}
Ok(())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment