Compare commits

..

2 Commits

Author SHA1 Message Date
Joey Pollack 6dc2052ce7 adds color role commands and meme commands 2 years ago
Joey Pollack e1583cc5c4 adds utils methods send_msg and add_role 2 years ago

1
.gitignore vendored

@ -14,6 +14,7 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information # MSVC Windows builds of rustc generate these, which store debugging information
*.pdb *.pdb
.vscode/
# SECRETS # SECRETS
secrets/ secrets/

@ -23,11 +23,13 @@ pub async fn parse(ctx: Context, msg: Message)
if server_id == utils::ADMIN_SERVER_ID && msg.channel_id == utils::ADMIN_COMMAND_CHANNEL_ID if server_id == utils::ADMIN_SERVER_ID && msg.channel_id == utils::ADMIN_COMMAND_CHANNEL_ID
{ {
test::parse_command(ctx, msg, server_id).await; test::parse_command(ctx, msg, server_id).await;
return;
} }
if server_id == utils::SM_SERVER_ID if server_id == utils::SM_SERVER_ID
{ {
sm_server::parse_command(ctx, msg, server_id).await;
return;
} }
} }
else else

@ -3,34 +3,111 @@ use serenity::prelude::*;
use serenity::model::prelude::*; use serenity::model::prelude::*;
use serenity::model::channel::Message; use serenity::model::channel::Message;
use crate::utils; use crate::utils;
// COLOR ROLE IDS
const ROLE_ID_RED: u64 = 279338933748236289;
const ROLE_ID_GREEN: u64 = 279338847102566400;
const ROLE_ID_BLUE: u64 = 279338904526782464;
const ROLE_ID_YELLOW: u64 = 279339011921805312;
const ROLE_ID_PINK: u64 = 279339051444862976;
const ROLE_ID_ORANGE: u64 = 279339077382438912;
const ROLE_ID_PURPLE: u64 = 284310454908747787;
pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId) pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId)
{ {
if msg.content == ".randomizer"
if handle_meme(ctx.clone(), msg.clone(), server_id).await
{ {
let item = utils::get_random_item(); return;
if let Err(why) = msg.channel_id.say(&ctx.http, format!("You found {}!", item)).await
{
println!("Error sending message: {:?}", why);
}
} }
let command_lower = msg.content.to_lowercase(); let command_lower = msg.content.to_lowercase();
let mut command_iter = command_lower.split_whitespace(); let mut command_iter = command_lower.split_whitespace();
let command_start = command_iter.next(); let command_start = command_iter.next();
if command_start == Some(".randomizer")
{
let item = utils::get_random_item();
utils::send_msg(ctx, msg.channel_id, &format!("You found {}!", item)).await;
return;
}
if command_start == Some(".color") if command_start == Some(".color")
{
utils::send_msg(ctx, msg.channel_id, "Valid colors are: green blue red purple pink yellow orange").await;
return;
}
if command_start == Some(".setcolor")
{ {
if let Some(arg) = command_iter.next() if let Some(arg) = command_iter.next()
{ {
match arg match arg
{ {
// TODO: add color roles // TODO: add color roles
"red" => { set_color(ctx, msg, RoleId(ROLE_ID_RED), server_id).await; return; }
"green" => { set_color(ctx, msg, RoleId(ROLE_ID_GREEN), server_id).await; return; }
"blue" => { set_color(ctx, msg, RoleId(ROLE_ID_BLUE), server_id).await; return; }
"yellow" => { set_color(ctx, msg, RoleId(ROLE_ID_YELLOW), server_id).await; return; }
"orange" => { set_color(ctx, msg, RoleId(ROLE_ID_ORANGE), server_id).await; return; }
"pink" => { set_color(ctx, msg, RoleId(ROLE_ID_PINK), server_id).await; return; }
"purple" => { set_color(ctx, msg, RoleId(ROLE_ID_PURPLE), server_id).await; return; }
_ => utils::Logger::log_error(ctx, &format!("unknown .color argument: {}", arg)).await _ => utils::Logger::log_error(ctx, &format!("unknown .color argument: {}", arg)).await
}; };
} }
} }
} }
///////////////////// HELPERS
async fn set_color(ctx: Context, msg: Message, role_id: RoleId, server_id: GuildId)
{
// Unset all color roles
let colors = [ROLE_ID_RED, ROLE_ID_GREEN, ROLE_ID_BLUE, ROLE_ID_YELLOW, ROLE_ID_PINK, ROLE_ID_ORANGE, ROLE_ID_PURPLE];
for color in colors
{
if let Err(why) = utils::remove_role(ctx.clone(), msg.author.clone().into(), RoleId(color), server_id).await
{
utils::send_msg(ctx.clone(), msg.channel_id, &format!("Could not set color")).await;
utils::Logger::log_error(ctx.clone(), &format!("set_color failed - could not remove other color roles: {}", why)).await;
return;
}
}
// Set requested color role
match utils::add_role(ctx.clone(), msg.author.into(), role_id, server_id).await
{
Ok(role) =>
{
utils::send_msg(ctx.clone(), msg.channel_id, &format!("Added role: {}", role.name)).await;
return;
}
Err(why) =>
{
utils::send_msg(ctx.clone(), msg.channel_id, &format!("Could not set color role")).await;
utils::Logger::log_error(ctx.clone(), &format!("set_color: Failed to set color role: {}", why)).await;
return;
}
}
}
async fn handle_meme(ctx: Context, msg: Message, server_id: GuildId) -> bool
{
let command_lower = msg.content.to_lowercase();
match command_lower.as_str()
{
".beer" => { utils::send_msg(ctx, msg.channel_id, "https://www.youtube.com/watch?v=IZVHBMCdXIw").await; return true }
".beeer" => { utils::send_msg(ctx, msg.channel_id, "https://youtu.be/nPtMHSYtPc4").await; return true }
".ivan" => { utils::send_msg(ctx, msg.channel_id, "http://i.imgur.com/0sjR5lE.jpg").await; return true }
".hotdog" => { utils::send_msg(ctx, msg.channel_id, "https://www.youtube.com/watch?v=ZXVhOPiM4mk").await; return true }
".edu" => { utils::send_msg(ctx, msg.channel_id, "<:ThisIsFine:243880310859759616> http://imgur.com/1STAzsl").await; return true }
".gari" => { utils::send_msg(ctx, msg.channel_id, "http://i.imgur.com/EYAti0a.jpg").await; return true }
".deerforce" => { utils::send_msg(ctx, msg.channel_id, "https://i.imgur.com/PSxFfku.png").await; return true }
_ => { return false; }
}
}

@ -11,6 +11,7 @@ use crate::utils;
pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId) pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId)
{ {
if server_id == utils::ADMIN_SERVER_ID if server_id == utils::ADMIN_SERVER_ID
{ {
if msg.content == ".log_test" if msg.content == ".log_test"
@ -18,32 +19,40 @@ pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId)
utils::Logger::log_message(ctx, "This is a test log message!").await; utils::Logger::log_message(ctx, "This is a test log message!").await;
return; return;
} }
if msg.content == ".shutdown"
{
return;
}
}
let command_lower = msg.content.to_lowercase();
let mut command_iter = command_lower.split_whitespace();
let command_start = command_iter.next();
if command_start == Some(".argstest")
{
if let Some(arg) = command_iter.next()
{
match arg
{
"red" => utils::send_msg(ctx, msg.channel_id, "arg was red!").await,
_ => utils::Logger::log_error(ctx, &format!("unknown .argstest argument: {}", arg)).await
};
}
return;
} }
if msg.content == ".role_test" if msg.content == ".role_test"
{ {
let test_role = RoleId(1129106677619228702 as u64); let test_role = RoleId(1129106677619228702 as u64);
if let Err(why) = server_id.edit_member(&ctx, msg.author, |m| match utils::add_role(ctx.clone(), msg.author.id, test_role, server_id).await
{ {
if let Some(mut member) = msg.member Ok(role) => utils::send_msg(ctx, msg.channel_id, &format!("Added role {:?}", role.name)).await,
{ Err(why) => utils::Logger::log_error(ctx, &format!("failed to add role to user - {}", why)).await,
member.roles.push(test_role);
return m.roles(member.roles);
}
m
}).await
{
utils::Logger::log_error(ctx, &format!("Error updating roles: {:?}", why)).await;
}
else
{
let role = &server_id.roles(&ctx).await.unwrap()[&test_role];
if let Err(why) = msg.channel_id.say(&ctx.http, format!("{} role added!", role)).await
{
utils::Logger::log_error(ctx, &format!("Error sending message: {:?}", why)).await;
}
}
return;
} }
return;
}
} }

@ -1,6 +1,6 @@
use rand::prelude::*; use rand::prelude::*;
use serenity::{client::Context, model::prelude::ChannelId}; use serenity::{client::Context, model::{user::User, prelude::{Role, ChannelId, GuildId, RoleId, UserId}, guild}};
pub const SM_SERVER_ID: u64 = 98929157894836224; pub const SM_SERVER_ID: u64 = 98929157894836224;
@ -9,10 +9,7 @@ pub const ADMIN_COMMAND_CHANNEL_ID: u64 = 1130607621699809443;
pub const MAIN_LOG_CHANNEL_ID: u64 = 1130607583007363074; pub const MAIN_LOG_CHANNEL_ID: u64 = 1130607583007363074;
pub const ERROR_LOG_CHANNEL_ID: u64 = 1130607602221461564; pub const ERROR_LOG_CHANNEL_ID: u64 = 1130607602221461564;
pub struct Logger pub struct Logger;
{
}
impl Logger impl Logger
{ {
@ -35,6 +32,55 @@ impl Logger
} }
} }
pub async fn send_msg(ctx: Context, channel: ChannelId, msg: &str)
{
if let Err(why) = channel.say(&ctx.http, msg).await
{
Logger::log_error(ctx, &format!("Error sending message: {:?}\nto channel: {:?}", why, channel)).await;
}
}
pub async fn add_role(ctx: Context, user_id: UserId, role_id: RoleId, server_id: GuildId) -> Result<Role, String>
{
if let Ok(mut member) = server_id.member(&ctx, user_id).await
{
if let Err(why) = member.add_role(&ctx.http, role_id).await
{
return Err(why.to_string());
}
match &server_id.roles(&ctx).await
{
Ok(roles) => return Ok(roles[&role_id].clone()),
Err(why) => return Err(why.to_string()),
}
}
else
{
return Err(String::from("Could not get member from the server"));
}
}
pub async fn remove_role(ctx: Context, user_id: UserId, role_id: RoleId, server_id: GuildId) -> Result<Role, String>
{
if let Ok(mut member) = server_id.member(&ctx, user_id).await
{
if let Err(why) = member.remove_role(&ctx.http, role_id).await
{
return Err(why.to_string());
}
match &server_id.roles(&ctx).await
{
Ok(roles) => return Ok(roles[&role_id].clone()),
Err(why) => return Err(why.to_string()),
}
}
else
{
return Err(String::from("Could not get member from the server"));
}
}
pub fn get_random_item() -> String pub fn get_random_item() -> String
{ {

Loading…
Cancel
Save