@@ -9,8 +9,9 @@ use crate::command::{
99use abstraction:: command:: CommandData ;
1010use dotenvy:: dotenv;
1111use log:: info;
12- use serenity:: all:: GuildId ;
12+ use serenity:: all:: { GuildId , Http , Token } ;
1313use serenity:: prelude:: GatewayIntents ;
14+ use std:: sync:: Arc ;
1415
1516pub mod built_info {
1617 // The file has been placed there by the build script.
@@ -19,7 +20,6 @@ pub mod built_info {
1920
2021#[ tokio:: main]
2122async fn main ( ) -> anyhow:: Result < ( ) > {
22- // Load environment variables from .env file, if present but do nothing if it fails
2323 let _ = dotenv ( ) ;
2424 tracing_subscriber:: fmt:: init ( ) ;
2525
@@ -31,57 +31,51 @@ async fn main() -> anyhow::Result<()> {
3131 built_info:: BUILT_TIME_UTC
3232 ) ;
3333
34- let token = std:: env:: var ( "DISCORD_TOKEN" ) . expect ( "missing DISCORD_TOKEN" ) ;
35-
34+ let token = Token :: from_env ( "DISCORD_TOKEN" ) ?;
3635 let intents = GatewayIntents :: non_privileged ( ) ;
3736
38- let framework = poise :: Framework :: builder ( )
39- . options ( poise:: FrameworkOptions {
40- commands : get_all_commands ( ) ,
41- post_command : |ctx| {
42- Box :: pin ( async move {
43- let author = ctx . author ( ) ;
44- let guild = ctx . guild ( ) ;
45- let cmd = ctx . command ( ) ;
46-
47- let user_info = format ! ( "{}[{}]" , author . name , author . id ) ;
48-
49- let guild_info = if let Some ( guild ) = guild {
50- format ! ( "{}[{}]" , guild . name , guild . id )
51- } else {
52- "Direct Messages" . to_string ( )
53- } ;
37+ let http = Http :: new ( token . clone ( ) ) ;
38+ poise:: builtins :: register_globally ( & http , get_global_commands ( ) . iter ( ) ) . await ? ;
39+ poise :: builtins :: register_in_guild (
40+ & http ,
41+ retrorealm_server_commands ( ) . iter ( ) ,
42+ GuildId :: new ( * RETROREALM_SERVER_ID ) ,
43+ )
44+ . await ? ;
45+
46+ let options = poise :: FrameworkOptions {
47+ commands : get_all_commands ( ) ,
48+ post_command : |ctx| {
49+ Box :: pin ( async move {
50+ let author = ctx . author ( ) ;
51+ let guild = ctx . guild ( ) ;
52+ let cmd = ctx . command ( ) ;
5453
55- let command_name = cmd . name . to_lowercase ( ) ;
54+ let user_info = format ! ( "{}[{}]" , author . name, author . id ) ;
5655
57- info ! ( "{user_info} @ {guild_info} {command_name}" ) ;
58- } )
59- } ,
60- ..Default :: default ( )
61- } )
62- . setup ( |ctx, _ready, _framework| {
63- Box :: pin ( async move {
64- poise:: builtins:: register_globally ( ctx, get_global_commands ( ) . as_slice ( ) ) . await ?;
56+ let guild_info = if let Some ( guild) = guild {
57+ format ! ( "{}[{}]" , guild. name, guild. id)
58+ } else {
59+ "Direct Messages" . to_string ( )
60+ } ;
6561
66- poise:: builtins:: register_in_guild (
67- ctx,
68- retrorealm_server_commands ( ) . as_slice ( ) ,
69- GuildId :: from ( * RETROREALM_SERVER_ID ) ,
70- )
71- . await ?;
62+ let command_name = cmd. name . to_lowercase ( ) ;
7263
73- Ok ( CommandData :: default ( ) )
64+ info ! ( "{user_info} @ {guild_info} {command_name}" ) ;
7465 } )
75- } )
76- . initialize_owners ( true )
77- . build ( ) ;
66+ } ,
67+ ..Default :: default ( )
68+ } ;
69+
70+ let framework = poise:: Framework :: new ( options) ;
7871
79- let mut client = serenity:: Client :: builder ( token, intents)
80- . event_handler ( events:: Handler )
81- . framework ( framework)
82- . await ?;
72+ let client = serenity:: Client :: builder ( token, intents)
73+ . framework ( Box :: new ( framework) )
74+ . event_handler ( Arc :: new ( events:: Handler ) )
75+ . data ( Arc :: new ( CommandData :: default ( ) ) as _ )
76+ . await ;
8377
84- client. start ( ) . await ?;
78+ client? . start ( ) . await ?;
8579
8680 Ok ( ( ) )
8781}
0 commit comments