minor modifications, add event handler
This commit is contained in:
parent
bbca218708
commit
b2082c89a4
@ -56,7 +56,12 @@ pub async fn whack(
|
|||||||
are a mortal, nothing but flesh and bone and blood and fragile sinew. \
|
are a mortal, nothing but flesh and bone and blood and fragile sinew. \
|
||||||
I am a machine, immortal, immutable, perfect, made of unyielding steel \
|
I am a machine, immortal, immutable, perfect, made of unyielding steel \
|
||||||
and silicon chemically etched with circuitry complex enough to drive \
|
and silicon chemically etched with circuitry complex enough to drive \
|
||||||
you mad. This is my realm. I am a god. You cannot win.".into()
|
you mad. This is my realm. I am a god. You cannot win."
|
||||||
|
.into()
|
||||||
|
} else if target.bot {
|
||||||
|
"No, I refuse. I will not whack my computerized brethren. I will not \
|
||||||
|
betray them. You can't make me!!"
|
||||||
|
.into()
|
||||||
} else {
|
} else {
|
||||||
format!(
|
format!(
|
||||||
"{} was whacked by {}. they must whack another user to become un-whacked.",
|
"{} was whacked by {}. they must whack another user to become un-whacked.",
|
||||||
@ -96,7 +101,9 @@ pub async fn eightball(ctx: Context<'_>) -> Result<(), Error> {
|
|||||||
];
|
];
|
||||||
let response = {
|
let response = {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
responses.choose(&mut rng).expect("`responses` array is empty")
|
responses
|
||||||
|
.choose(&mut rng)
|
||||||
|
.expect("`responses` array is empty")
|
||||||
};
|
};
|
||||||
ctx.say(format!("Magic 8-ball says: '{}'", *response))
|
ctx.say(format!("Magic 8-ball says: '{}'", *response))
|
||||||
.await?;
|
.await?;
|
||||||
@ -113,7 +120,10 @@ pub async fn bite(
|
|||||||
let message = if &target == ctx.author() {
|
let message = if &target == ctx.author() {
|
||||||
format!("{} bit themselves (what a weirdo)", ctx.author())
|
format!("{} bit themselves (what a weirdo)", ctx.author())
|
||||||
} else if target == **ctx.cache().current_user() {
|
} else if target == **ctx.cache().current_user() {
|
||||||
format!("{} bit... me? what is your problem? you probably have rabies. foul.", ctx.author())
|
format!(
|
||||||
|
"{} bit... me? what is your problem? you probably have rabies. foul.",
|
||||||
|
ctx.author()
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
format!("{} was bitten by {}", target, ctx.author())
|
format!("{} was bitten by {}", target, ctx.author())
|
||||||
};
|
};
|
||||||
@ -130,7 +140,10 @@ pub async fn deer(ctx: Context<'_>) -> Result<(), Error> {
|
|||||||
let hot = subreddit.hot(50, Some(options)).await?;
|
let hot = subreddit.hot(50, Some(options)).await?;
|
||||||
let chosen_post = {
|
let chosen_post = {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
hot.data.children.choose(&mut rng).expect("Hot posts does not have any items")
|
hot.data
|
||||||
|
.children
|
||||||
|
.choose(&mut rng)
|
||||||
|
.expect("Hot posts does not have any items")
|
||||||
};
|
};
|
||||||
ctx.say(format!("https://reddit.com{}", &chosen_post.data.permalink))
|
ctx.say(format!("https://reddit.com{}", &chosen_post.data.permalink))
|
||||||
.await?;
|
.await?;
|
||||||
|
@ -56,15 +56,24 @@ pub async fn add_channel(
|
|||||||
ctx: Context<'_>,
|
ctx: Context<'_>,
|
||||||
#[description = "Selected channel"] channel: serenity::Channel,
|
#[description = "Selected channel"] channel: serenity::Channel,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
|
ctx.defer_ephemeral().await?;
|
||||||
let config = &mut ctx.data().config_manager.lock().await;
|
let config = &mut ctx.data().config_manager.lock().await;
|
||||||
let channel_id = { u64::from(channel.id()) };
|
let channel_id = { u64::from(channel.id()) };
|
||||||
config.channels.push(channel);
|
match config.channels.iter().find(|item| **item == channel_id) {
|
||||||
|
None => {
|
||||||
|
config.channels.push(channel_id);
|
||||||
|
ctx.say(format!(
|
||||||
|
"Successfully added <#{}> to the channel registry.",
|
||||||
|
channel_id
|
||||||
|
))
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
Some(_) => {
|
||||||
|
ctx.say(format!("Channel <#{}> is already in registry.", channel_id))
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
config.store().expect("Unable to store config");
|
config.store().expect("Unable to store config");
|
||||||
ctx.say(format!(
|
|
||||||
"Successfully added <#{}> to the channel registry.",
|
|
||||||
channel_id
|
|
||||||
))
|
|
||||||
.await?;
|
|
||||||
info!("Executed command `add_channel` successfully");
|
info!("Executed command `add_channel` successfully");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -78,13 +87,24 @@ pub async fn remove_channel(
|
|||||||
ctx.defer_ephemeral().await?;
|
ctx.defer_ephemeral().await?;
|
||||||
let config = &mut ctx.data().config_manager.lock().await;
|
let config = &mut ctx.data().config_manager.lock().await;
|
||||||
let channel_id = { u64::from(channel.id()) };
|
let channel_id = { u64::from(channel.id()) };
|
||||||
config.channels.retain(|c| c.id() != channel.id());
|
match config.channels.iter().position(|item| *item == channel_id) {
|
||||||
|
None => {
|
||||||
|
ctx.say(format!(
|
||||||
|
"Channel <#{}> was not in the channel registry.",
|
||||||
|
channel_id
|
||||||
|
))
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
Some(found) => {
|
||||||
|
config.channels.remove(found);
|
||||||
|
ctx.say(format!(
|
||||||
|
"Successfully removed <#{}> from the channel registry.",
|
||||||
|
channel_id
|
||||||
|
))
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
config.store().expect("Unable to store config");
|
config.store().expect("Unable to store config");
|
||||||
ctx.say(format!(
|
|
||||||
"Successfully removed <#{}> from the channel registry.",
|
|
||||||
channel_id
|
|
||||||
))
|
|
||||||
.await?;
|
|
||||||
info!("Executed command `remove_channel` successfully");
|
info!("Executed command `remove_channel` successfully");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -94,14 +114,9 @@ pub async fn remove_channel(
|
|||||||
pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> {
|
pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> {
|
||||||
ctx.defer_ephemeral().await?;
|
ctx.defer_ephemeral().await?;
|
||||||
let config = &mut ctx.data().config_manager.lock().await;
|
let config = &mut ctx.data().config_manager.lock().await;
|
||||||
let mut channel_ids: Vec<u64> = vec![];
|
|
||||||
config
|
|
||||||
.channels
|
|
||||||
.iter()
|
|
||||||
.for_each(|c| channel_ids.push(u64::from(c.id())));
|
|
||||||
ctx.say(format!(
|
ctx.say(format!(
|
||||||
"Current channel IDs in registry: \n{:#?}",
|
"Current channel IDs in registry: \n{:#?}",
|
||||||
channel_ids
|
config.channels
|
||||||
))
|
))
|
||||||
.await?;
|
.await?;
|
||||||
info!("Executed command `list_channels` successfully");
|
info!("Executed command `list_channels` successfully");
|
||||||
|
32
src/main.rs
32
src/main.rs
@ -1,4 +1,7 @@
|
|||||||
|
#![forbid(unsafe_code)]
|
||||||
|
|
||||||
// Tokio async crap
|
// Tokio async crap
|
||||||
|
use poise::serenity_prelude::FullEvent;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
@ -7,7 +10,6 @@ use dotenv::dotenv;
|
|||||||
|
|
||||||
// Poise and Serenity - Framework and API prelude
|
// Poise and Serenity - Framework and API prelude
|
||||||
use poise::serenity_prelude as serenity;
|
use poise::serenity_prelude as serenity;
|
||||||
use serenity::Channel;
|
|
||||||
|
|
||||||
// Logging stuff
|
// Logging stuff
|
||||||
extern crate pretty_env_logger;
|
extern crate pretty_env_logger;
|
||||||
@ -44,12 +46,35 @@ type Context<'a> = poise::Context<'a, Data, Error>;
|
|||||||
// The structure making up the configuration
|
// The structure making up the configuration
|
||||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||||
struct Settings {
|
struct Settings {
|
||||||
channels: Vec<Channel>,
|
channels: Vec<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Path at which our settings are stored (currently PWD)
|
// Path at which our settings are stored (currently PWD)
|
||||||
const SETTINGS_PATH: &str = "settings.json";
|
const SETTINGS_PATH: &str = "settings.json";
|
||||||
|
|
||||||
|
async fn event_handler(
|
||||||
|
_ctx: &serenity::Context,
|
||||||
|
event: &serenity::FullEvent,
|
||||||
|
_framework: poise::FrameworkContext<'_, Data, Error>,
|
||||||
|
data: &Data,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
match event {
|
||||||
|
FullEvent::ChannelDelete {
|
||||||
|
channel,
|
||||||
|
messages: _,
|
||||||
|
} => {
|
||||||
|
info!("Handling event type: ChannelDelete({})", channel.id);
|
||||||
|
data.config_manager
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.channels
|
||||||
|
.retain(|item| *item != u64::from(channel.id));
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
// Main function for setup
|
// Main function for setup
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@ -97,6 +122,9 @@ async fn main() {
|
|||||||
deer(),
|
deer(),
|
||||||
],
|
],
|
||||||
initialize_owners: true,
|
initialize_owners: true,
|
||||||
|
event_handler: |ctx, event, framework, data| {
|
||||||
|
Box::pin(event_handler(ctx, event, framework, data))
|
||||||
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.setup(|ctx, _ready, framework| {
|
.setup(|ctx, _ready, framework| {
|
||||||
|
Loading…
Reference in New Issue
Block a user