adds message return value from utils::send_msg, and test command

main
Joey Pollack 2 years ago
parent 018855a9a7
commit 4dcb65e863

@ -1,6 +1,6 @@
// use serenity::async_trait; // use serenity::async_trait;
use serenity::prelude::*; use serenity::prelude::*;
use serenity::model::channel::Message; use serenity::model::channel::{Message, Reaction};
// use serenity::model::gateway::Ready; // use serenity::model::gateway::Ready;
// use serenity::model::prelude::*; // use serenity::model::prelude::*;
@ -10,6 +10,12 @@ pub mod sm_server;
use crate::utils; use crate::utils;
pub async fn initialize(ctx: Context) -> Result<(), String>
{
Ok(())
}
pub async fn parse(ctx: Context, msg: Message) pub async fn parse(ctx: Context, msg: Message)
{ {
if let Some(server_id) = msg.guild_id if let Some(server_id) = msg.guild_id
@ -37,3 +43,26 @@ pub async fn parse(ctx: Context, msg: Message)
utils::Logger::log_message(ctx, &format!("DM Received from {}: {}", msg.author.name, msg.content)).await; utils::Logger::log_message(ctx, &format!("DM Received from {}: {}", msg.author.name, msg.content)).await;
} }
} }
pub async fn handle_reaction_add(ctx: Context, add_reaction: Reaction)
{
if let Some(server_id) = add_reaction.guild_id
{
if server_id == utils::ADMIN_SERVER_ID && add_reaction.channel_id == utils::ADMIN_COMMAND_CHANNEL_ID
{
//test::handle_reaction_add(ctx, add_reaction, server_id).await;
return;
}
if server_id == utils::SM_SERVER_ID
{
sm_server::handle_reaction_add(ctx, add_reaction).await;
return;
}
}
}
pub async fn handle_reaction_remove(ctx: Context, removed_reaction: Reaction)
{
}

@ -3,7 +3,9 @@ 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, Owner};
const SM_TEST_CHANNEL_ID: u64 = 280548893135994880;
// COLOR ROLE IDS // COLOR ROLE IDS
const ROLE_ID_RED: u64 = 279338933748236289; const ROLE_ID_RED: u64 = 279338933748236289;
@ -14,6 +16,9 @@ const ROLE_ID_PINK: u64 = 279339051444862976;
const ROLE_ID_ORANGE: u64 = 279339077382438912; const ROLE_ID_ORANGE: u64 = 279339077382438912;
const ROLE_ID_PURPLE: u64 = 284310454908747787; const ROLE_ID_PURPLE: u64 = 284310454908747787;
// RACE ROLE IDS
pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId) pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId)
{ {
@ -27,17 +32,36 @@ pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId)
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();
let data = ctx.data.read().await;
let owner_id = match data.get::<Owner>()
{
Some(o) => o.load(std::sync::atomic::Ordering::Relaxed),
None => { utils::Logger::log_error(ctx.clone(), &format!("Couldn't get bot owner id")).await; return; }
};
if UserId::from(owner_id) == msg.author.id && command_start == Some(".dotestmsg")
{
if let Some(msg) = utils::send_msg(ctx.clone(), ChannelId::from(SM_TEST_CHANNEL_ID), "Test message!").await
{
println!("Test message id is: {:#?}", msg.id)
}
else
{
println!("Failed to get test message id");
}
}
if command_start == Some(".randomizer") if command_start == Some(".randomizer")
{ {
let item = utils::get_random_item(); let item = utils::get_random_item();
utils::send_msg(ctx, msg.channel_id, &format!("You found {}!", item)).await; utils::send_msg(ctx.clone(), msg.channel_id, &format!("You found {}!", item)).await;
return; return;
} }
if command_start == Some(".colors") || command_start == Some(".color") if command_start == Some(".colors") || command_start == Some(".color")
{ {
utils::send_msg(ctx, msg.channel_id, "Valid colors are: green blue red purple pink yellow orange").await; utils::send_msg(ctx.clone(), msg.channel_id, "Valid colors are: green blue red purple pink yellow orange").await;
return; return;
} }
@ -48,20 +72,25 @@ pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId)
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; } "red" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_RED), server_id).await; return; }
"green" => { set_color(ctx, msg, RoleId(ROLE_ID_GREEN), server_id).await; return; } "green" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_GREEN), server_id).await; return; }
"blue" => { set_color(ctx, msg, RoleId(ROLE_ID_BLUE), server_id).await; return; } "blue" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_BLUE), server_id).await; return; }
"yellow" => { set_color(ctx, msg, RoleId(ROLE_ID_YELLOW), server_id).await; return; } "yellow" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_YELLOW), server_id).await; return; }
"orange" => { set_color(ctx, msg, RoleId(ROLE_ID_ORANGE), server_id).await; return; } "orange" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_ORANGE), server_id).await; return; }
"pink" => { set_color(ctx, msg, RoleId(ROLE_ID_PINK), server_id).await; return; } "pink" => { set_color(ctx.clone(), msg, RoleId(ROLE_ID_PINK), server_id).await; return; }
"purple" => { set_color(ctx, msg, RoleId(ROLE_ID_PURPLE), server_id).await; return; } "purple" => { set_color(ctx.clone(), 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.clone(), &format!("unknown .color argument: {}", arg)).await
}; };
} }
} }
} }
pub async fn handle_reaction_add(ctx: Context, reaction_add: Reaction)
{
}
///////////////////// HELPERS ///////////////////// HELPERS
async fn set_color(ctx: Context, msg: Message, role_id: RoleId, server_id: GuildId) async fn set_color(ctx: Context, msg: Message, role_id: RoleId, server_id: GuildId)
{ {

@ -34,12 +34,14 @@ pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId)
{ {
if let Some(arg) = command_iter.next() if let Some(arg) = command_iter.next()
{ {
match arg if arg == "red"
{ {
"red" => utils::send_msg(ctx, msg.channel_id, "arg was red!").await, utils::send_msg(ctx, msg.channel_id, "arg was red!").await.unwrap();
}
_ => utils::Logger::log_error(ctx, &format!("unknown .argstest argument: {}", arg)).await else
}; {
utils::Logger::log_error(ctx, &format!("unknown .argstest argument: {}", arg)).await
}
} }
return; return;
} }
@ -49,8 +51,8 @@ pub async fn parse_command(ctx: Context, msg: Message, server_id: GuildId)
let test_role = RoleId(1129106677619228702 as u64); let test_role = RoleId(1129106677619228702 as u64);
match utils::add_role(ctx.clone(), msg.author.id, test_role, server_id).await match utils::add_role(ctx.clone(), msg.author.id, test_role, server_id).await
{ {
Ok(role) => utils::send_msg(ctx, msg.channel_id, &format!("Added role {:?}", role.name)).await, Ok(role) => { utils::send_msg(ctx, msg.channel_id, &format!("Added role {:?}", role.name)).await.unwrap(); }
Err(why) => utils::Logger::log_error(ctx, &format!("failed to add role to user - {}", why)).await, Err(why) => { utils::Logger::log_error(ctx, &format!("failed to add role to user - {}", why)).await; }
} }
return; return;

@ -46,9 +46,11 @@ impl EventHandler for Handler
async fn reaction_add(&self, ctx: Context, add_reaction: Reaction) async fn reaction_add(&self, ctx: Context, add_reaction: Reaction)
{ {
println!("reaction_add event"); // println!("reaction_add event");
utils::Logger::log_message(ctx, &format!("reaction add event: {:#?}", add_reaction)).await; // utils::Logger::log_message(ctx, &format!("reaction add event: {:#?}", add_reaction)).await;
// todo!() // todo!()
commands::handle_reaction_add(ctx, add_reaction).await;
} }
async fn reaction_remove(&self, ctx: Context, removed_reaction: Reaction) async fn reaction_remove(&self, ctx: Context, removed_reaction: Reaction)
@ -66,7 +68,16 @@ impl EventHandler for Handler
async fn ready(&self, ctx: Context, ready: Ready) async fn ready(&self, ctx: Context, ready: Ready)
{ {
println!("{} is connected!", ready.user.name); println!("{} is connected!", ready.user.name);
utils::Logger::log_message(ctx, "Start up and connection successful!").await; utils::Logger::log_message(ctx.clone(), "Start up and connection successful!").await;
if let Err(why) = commands::initialize(ctx.clone()).await
{
utils::Logger::log_error(ctx.clone(), &format!("Failed to initialize commands module: {}", why)).await;
}
else
{
utils::Logger::log_message(ctx.clone(), "Module initialization successful!").await;
}
} }
// CODE TAKEN FROM EXAMPLE: // CODE TAKEN FROM EXAMPLE:

@ -1,6 +1,6 @@
use rand::prelude::*; use rand::prelude::*;
use serenity::{client::Context, model::prelude::{Role, ChannelId, GuildId, RoleId, UserId}}; use serenity::{client::Context, model::{channel::Message, prelude::{ChannelId, GuildId, Role, RoleId, UserId}}};
pub const SM_SERVER_ID: u64 = 98929157894836224; pub const SM_SERVER_ID: u64 = 98929157894836224;
@ -32,12 +32,26 @@ impl Logger
} }
} }
pub async fn send_msg(ctx: Context, channel: ChannelId, msg: &str) pub async fn send_msg(ctx: Context, channel: ChannelId, msg: &str) -> Option<Message>
{ {
if let Err(why) = channel.say(&ctx.http, msg).await
match channel.say(&ctx.http, msg).await
{
Ok(m) => { return Some(m); },
Err(why) =>
{ {
Logger::log_error(ctx, &format!("Error sending message: {:?}\nto channel: {:?}", why, channel)).await; Logger::log_error(ctx, &format!("Error sending message: {:?}\nto channel: {:?}", why, channel)).await;
return None;
}
} }
// if let Err(why) = channel.say(&ctx.http, msg).await
// {
// Logger::log_error(ctx, &format!("Error sending message: {:?}\nto channel: {:?}", why, channel)).await;
// return None;
// }
} }
pub async fn add_role(ctx: Context, user_id: UserId, role_id: RoleId, server_id: GuildId) -> Result<Role, String> pub async fn add_role(ctx: Context, user_id: UserId, role_id: RoleId, server_id: GuildId) -> Result<Role, String>

Loading…
Cancel
Save