Skip to content

Commit

Permalink
Make bot usable in DMs
Browse files Browse the repository at this point in the history
  • Loading branch information
Scotsguy committed Feb 26, 2020
1 parent 3f2225e commit 57d9981
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
32 changes: 29 additions & 3 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serenity::{
macros::{command, group},
Args, CommandResult,
},
model::channel::Message,
model::{channel::Message, id::UserId},
prelude::*,
utils::Colour,
};
Expand All @@ -19,7 +19,6 @@ macro_rules! static_text_command {
$(
#[command]
$( #[aliases($($aliases),+)] )?
#[only_in("guilds")]
fn $name(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult {
if let Err(why) = msg.channel_id.send_message(&ctx.http, |m| {
m.embed(|e| {
Expand Down Expand Up @@ -50,7 +49,6 @@ macro_rules! static_image_command {
$(
#[command]
$( #[aliases($($aliases),+)] )?
#[only_in("guilds")]
fn $name(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult {
if let Err(why) = msg.channel_id.send_message(&ctx.http, |m| {
m.embed(|e| {
Expand Down Expand Up @@ -86,3 +84,31 @@ static_image_command! {
select_java sjava, "https://cdn.discordapp.com/attachments/531598137790562305/575378380573114378/unknown.png", "Please select your Java version in the MultiMC settings:";
select_memory smemory sram, "https://cdn.discordapp.com/attachments/531598137790562305/575376840173027330/unknown.png", "Please set your instance memory allocation:";
}

#[group]
#[commands(info)]
struct Other;

#[command]
fn info(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult {
if let Err(why) = msg.channel_id.send_message(&ctx.http, |m| {
m.embed(|e| {
e.title("<:backgroundcat:280120125284417536>A bot to parse logfiles on the MultiMC discord<:backgroundcat:280120125284417536>");
let creator_name = match UserId::from(185_461_862_878_543_872).to_user(&ctx) {
Ok(o) => o.tag(),
Err(why) => {error!("Couldn't get info about creator: {}", why); "<Error getting name>".to_string()}
};
e.colour(Colour::DARK_TEAL);
e.description(format!(r"
Developed by {}.
To start, just upload a log from MultiMC. (Type `-log` for help)
[Source Code available under AGPLv3](https://gitlab.com/Scotsguy/background-cat)
", creator_name))
});
m
}) {
error!("Couldn't send info message: {}", why)
}
Ok(())
}
63 changes: 28 additions & 35 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use log::{debug, error, info};
use rayon::prelude::*;
use regex::Regex;
use reqwest::blocking::get;
use std::{collections::HashSet, env};

use serenity::{
framework::standard::{
Expand All @@ -18,22 +19,39 @@ mod parsers;
use parsers::PARSERS;

mod commands;
use commands::{STATICIMAGE_GROUP, STATICTEXT_GROUP};
use std::collections::HashSet;
use commands::{OTHER_GROUP, STATICIMAGE_GROUP, STATICTEXT_GROUP};

struct StringContainer;

impl TypeMapKey for StringContainer {
type Value = String;
}

fn main() {
kankyo::load(false).expect("Expected a .env file");
env_logger::init();

let token = std::env::var("DISCORD_TOKEN").expect("Expected a token in $DISCORD_TOKEN");
let token = env::var("DISCORD_TOKEN").expect("Expected a token in $DISCORD_TOKEN");

let mut client = Client::new(&token, Handler).expect("Err creating client");

{
let mut data = client.data.write();
data.insert::<StringContainer>(
match client.cache_and_http.http.get_user(185_461_862_878_543_872) {
Ok(o) => o.tag(),
Err(why) => {
error!("Couldn't get info about creator: {}", why);
"<Error getting name>".to_string()
}
},
);
}

client.with_framework(
StandardFramework::new()
.configure(|c| {
c.with_whitespace(true)
.allow_dm(false)
.on_mention(Some(
client
.cache_and_http
Expand All @@ -42,13 +60,12 @@ fn main() {
.expect("couldn't get info on the bot user")
.id,
))
.prefix(
&std::env::var("BACKGROUND_CAT_PREFIX").unwrap_or_else(|_| "-".to_string()),
)
.prefix(&env::var("BACKGROUND_CAT_PREFIX").unwrap_or_else(|_| "-".to_string()))
.case_insensitivity(true)
})
.group(&STATICTEXT_GROUP)
.group(&STATICIMAGE_GROUP)
.group(&OTHER_GROUP)
.help(&MY_HELP),
);

Expand Down Expand Up @@ -139,41 +156,17 @@ impl EventHandler for Handler {
}
return;
};

if msg.is_private() {
if let Err(why) = msg.channel_id.send_message(&ctx.http, |m| {
m.embed(|e| {
e.title("<:backgroundcat:280120125284417536>A bot to parse logfiles on the MultiMC discord<:backgroundcat:280120125284417536>");
#[allow(clippy::unreadable_literal)]
let creator_name = match UserId::from(185461862878543872).to_user(&ctx) {
Ok(o) => o.tag(),
Err(why) => {error!("Couldn't get info about creator: {}", why); "<Error getting name>".to_string()}
};
e.colour(Colour::DARK_TEAL);
e.description(format!(r"
Developed by {}.
To start, just post a https://paste.ee link in the Discord.
[Source Code available under AGPLv3](https://gitlab.com/Scotsguy/background-cat)
", creator_name));
e
});
m
}) {
error!("Couldn't send info message: {}", why)
}
}
}

// TODO: delete on reaction

fn ready(&self, ctx: Context, ready: Ready) {
use serenity::model::{gateway::Activity, user::OnlineStatus};

info!(
"{}#{} is connected!",
ready.user.name, ready.user.discriminator
info!("{} is connected!", ready.user.tag());
ctx.set_presence(
Some(Activity::playing("DM me: -info")),
OnlineStatus::Online,
);
ctx.set_presence(Some(Activity::playing("DM me!")), OnlineStatus::Online);
}
}

0 comments on commit 57d9981

Please sign in to comment.