2023-12-04 16:35:42 +11:00
mod commands ;
2024-11-27 21:50:28 +11:00
mod shutdown ;
2024-08-09 18:19:59 -04:00
// https://cdn.toast-server.net/RustFSHiearchy.png
// Using the new filesystem hierarchy
2024-11-27 21:50:28 +11:00
use rustbot_tokens ::discord_token ;
2024-03-21 09:03:09 +11:00
use poise ::serenity_prelude ::{
2024-10-21 17:36:18 +11:00
builder ::CreateAllowedMentions ,
2024-01-12 13:59:46 +11:00
ClientBuilder ,
2024-10-21 17:36:18 +11:00
ActivityData ,
2024-01-12 13:59:46 +11:00
GatewayIntents
} ;
2024-10-21 17:36:18 +11:00
use rustbot_lib ::{
utils ::{
mention_dev ,
get_guild_name
} ,
RustbotData ,
config ::BINARY_PROPERTIES
} ;
use rustbot_events ::events ::processor ;
use std ::{
sync ::Arc ,
borrow ::Cow
} ;
2024-08-09 18:19:59 -04:00
2023-12-04 16:35:42 +11:00
#[ tokio::main ]
async fn main ( ) {
2024-10-21 17:36:18 +11:00
let prefix = if BINARY_PROPERTIES . env . contains ( " prod " ) {
Some ( Cow ::Borrowed ( " pg. " ) )
} else {
Some ( Cow ::Borrowed ( " pg! " ) )
} ;
2023-12-04 16:35:42 +11:00
2024-10-21 17:36:18 +11:00
let commands = commands ::collect! ( ) ;
2024-01-12 13:59:46 +11:00
let framework = poise ::Framework ::builder ( )
2023-12-04 16:35:42 +11:00
. options ( poise ::FrameworkOptions {
2024-10-21 17:36:18 +11:00
commands ,
2023-12-08 12:34:57 +11:00
pre_command : | ctx | Box ::pin ( async move {
2024-10-21 17:36:18 +11:00
let get_guild_channel_name = match ctx . guild_channel ( ) . await {
Some ( channel ) = > format! ( " in # {} " , channel . name . clone ( ) ) ,
None = > String ::from ( " " )
} ;
let prefix = match ctx . command ( ) . prefix_action {
Some ( _ ) = > ctx . framework ( ) . options . prefix_options . prefix . as_ref ( ) . unwrap ( ) ,
None = > " / "
2023-12-10 21:03:52 +11:00
} ;
2024-10-21 17:36:18 +11:00
println! (
2024-11-27 21:50:28 +11:00
" Discord[{}:S{}]: {} ran {prefix}{} {get_guild_channel_name} " ,
2024-10-21 17:36:18 +11:00
get_guild_name ( ctx ) ,
ctx . serenity_context ( ) . shard_id ,
ctx . author ( ) . name ,
ctx . command ( ) . qualified_name ,
2024-11-27 21:50:28 +11:00
) ;
2023-12-10 21:03:52 +11:00
} ) ,
2024-10-21 17:36:18 +11:00
prefix_options : poise ::PrefixFrameworkOptions {
prefix ,
ignore_bots : true ,
mention_as_prefix : false ,
2024-11-27 21:50:28 +11:00
case_insensitive_commands : true ,
execute_self_messages : false ,
2024-10-21 17:36:18 +11:00
.. Default ::default ( )
} ,
2024-03-11 10:06:40 +11:00
on_error : | error | Box ::pin ( async move {
match error {
poise ::FrameworkError ::Command { error , ctx , .. } = > {
println! ( " PoiseCommandError( {} ): {} " , ctx . command ( ) . qualified_name , error ) ;
2024-08-09 18:19:59 -04:00
ctx . reply ( format! (
" Encountered an error during command execution, ask {} to check console for more details! " ,
mention_dev ( ctx ) . unwrap_or_default ( )
) ) . await . expect ( " Error sending message " ) ;
} ,
poise ::FrameworkError ::EventHandler { error , event , .. } = > println! ( " PoiseEventHandlerError( {} ): {} " , event . snake_case_name ( ) , error ) ,
2024-10-21 17:36:18 +11:00
poise ::FrameworkError ::NotAnOwner { ctx , .. } = > {
println! ( " PoiseNotAnOwner: {} tried to execute a developer-level command ( {} ) " , ctx . author ( ) . name , ctx . command ( ) . qualified_name ) ;
ctx . reply ( " Whoa, you discovered a hidden command! Too bad, I can't allow you to execute it as you're not my creator. " ) . await . expect ( " Error sending message " ) ;
} ,
2024-08-09 18:19:59 -04:00
poise ::FrameworkError ::UnknownInteraction { interaction , .. } = > println! (
" PoiseUnknownInteractionError: {} tried to execute an unknown interaction ({}) " ,
interaction . user . name ,
interaction . data . name
) ,
2024-10-21 17:36:18 +11:00
poise ::FrameworkError ::UnknownCommand { msg , .. } = > println! (
" PoiseUnknownCommandError: {} tried to execute an unknown command ({}) " ,
msg . author . name ,
msg . content
) ,
poise ::FrameworkError ::ArgumentParse { ctx , error , .. } = > {
println! ( " PoiseArgumentParseError: {} " , error ) ;
ctx . reply ( format! ( " Error parsing argument(s): {error} " ) ) . await . expect ( " Error sending message " ) ;
} ,
poise ::FrameworkError ::CommandPanic { ctx , payload , .. } = > {
if let Some ( payload ) = payload . clone ( ) {
println! ( " PoiseCommandPanic: {payload} " ) ;
ctx . reply ( format! (
" The command panicked, please tell my developer about this! \n **Error:**``` \n {payload} \n ``` "
) ) . await . expect ( " Error sending message " ) ;
} else {
println! ( " PoiseCommandPanic: No payload provided " ) ;
let uh_oh = [
" Well, this is concerning... Hopefully you notified my developer about this! " ,
" The command panicked, but didn't leave any trace behind... Suspicious! " ,
] . join ( " \n " ) ;
ctx . reply ( uh_oh ) . await . expect ( " Error sending message " ) ;
}
} ,
other = > println! ( " PoiseOtherError: {other} " )
2024-03-11 10:06:40 +11:00
}
} ) ,
2024-10-21 17:36:18 +11:00
allowed_mentions : Some ( CreateAllowedMentions ::default ( ) . empty_users ( ) ) ,
2024-03-11 10:06:40 +11:00
initialize_owners : true ,
2024-10-21 17:36:18 +11:00
skip_checks_for_owners : true ,
event_handler : | framework , event | Box ::pin ( processor ( framework , event ) ) ,
2023-12-04 16:35:42 +11:00
.. Default ::default ( )
2023-12-10 21:03:52 +11:00
} )
2024-01-12 13:59:46 +11:00
. build ( ) ;
2024-08-09 18:19:59 -04:00
let mut client = ClientBuilder ::new (
2024-11-27 21:50:28 +11:00
discord_token ( ) . await ,
2024-08-09 18:19:59 -04:00
GatewayIntents ::GUILDS
2024-10-21 17:36:18 +11:00
| GatewayIntents ::GUILD_MESSAGES
| GatewayIntents ::MESSAGE_CONTENT
2024-08-09 18:19:59 -04:00
)
. framework ( framework )
2024-10-25 04:06:10 +11:00
. data ( Arc ::new ( RustbotData { } ) )
2024-10-21 17:36:18 +11:00
. activity ( ActivityData ::custom ( " nep nep! " ) )
2024-08-09 18:19:59 -04:00
. await . expect ( " Error creating client " ) ;
2023-12-04 16:35:42 +11:00
2024-11-27 21:50:28 +11:00
let shard_manager = client . shard_manager . clone ( ) ;
tokio ::spawn ( async move {
shutdown ::gracefully_shutdown ( ) . await ;
shard_manager . shutdown_all ( ) . await ;
} ) ;
2024-10-21 17:36:18 +11:00
if let Err ( why ) = client . start_autosharded ( ) . await {
2024-11-27 21:50:28 +11:00
println! ( " Error starting client: {why:#?} " ) ;
2023-12-04 16:35:42 +11:00
}
}