12
12
from dotenv import load_dotenv #
13
13
import random #
14
14
from discord .ext import commands #
15
+ import logging #
16
+ import time #
15
17
#
16
18
#####################################
17
19
20
+
18
21
#####################################
19
22
#
20
23
COMMAND_PREFIX = '!' #
21
24
VERSION = "v0.1-alpha" #
22
25
ACTIVITY = discord .Game ("!help" ) #
26
+ LOG_LEVEL = logging .INFO #
23
27
#
24
28
#####################################
25
29
30
+
31
+ # Load bot token from .env
26
32
load_dotenv ()
27
33
TOKEN = os .getenv ('DISCORD_TOKEN' )
28
34
29
- bot = commands .Bot (command_prefix = COMMAND_PREFIX )
35
+ # Initialize bot object to use the COMMAND_PREFIX defined above
36
+ bot = commands .Bot (command_prefix = COMMAND_PREFIX )
37
+
38
+ # Generate timestamp of startup
39
+ timestamp = time .strftime ('%Y%m%d-%H%M%S' )
30
40
41
+ # Configure logging
42
+ logging .basicConfig (
43
+ level = LOG_LEVEL ,
44
+ format = '%(asctime)s: [%(levelname)s] - %(message)s' ,
45
+ datefmt = '%Y-%m-%d %H:%M:%S' ,
46
+ handlers = [
47
+ logging .FileHandler (f"logs/{ timestamp } .log" , mode = "w" ),
48
+ logging .StreamHandler ()
49
+ ]
50
+ )
31
51
32
52
@bot .event
33
53
async def on_connect ():
@@ -36,7 +56,7 @@ async def on_connect():
36
56
Sets the bot activity to ACTIVITY.
37
57
"""
38
58
39
- print (f'{ bot .user .name } { VERSION } has sucessfully connected to Discord.' )
59
+ logging . warning (f'{ bot .user .name } { VERSION } has sucessfully connected to Discord.' )
40
60
await bot .change_presence (activity = ACTIVITY )
41
61
42
62
@@ -47,14 +67,11 @@ async def on_ready():
47
67
date from Discord servers.
48
68
"""
49
69
50
- print ('Bot loading complete. Current guilds: ' , end = ' ' )
70
+ logging . info ('Bot loading complete. Current guilds: ' )
51
71
52
- guilds = []
53
72
for guild in bot .guilds :
54
73
label = guild .name + " (" + str (guild .id ) + ")"
55
- guilds .append (label )
56
-
57
- print (* guilds , sep = ', ' )
74
+ logging .info (label )
58
75
59
76
60
77
@bot .event
@@ -63,7 +80,7 @@ async def on_disconnect():
63
80
Prints a message when bot disconnects from Discord. Usually this is temporary.
64
81
"""
65
82
66
- print ('Lost connection to Discord.' )
83
+ logging . warning ('Lost connection to Discord.' )
67
84
68
85
69
86
@@ -88,11 +105,10 @@ async def on_error(event, *args, **kwargs):
88
105
Writes to err.log whenever a message triggers an error
89
106
"""
90
107
91
- with open ('err.log' , 'a' , encoding = 'utf-8' ) as errfile :
92
- if event == 'on_message' :
93
- errfile .write (f'Unhandled message: { args [0 ]} \n ' )
94
- else :
95
- raise
108
+ if event == 'on_message' :
109
+ logging .error (f'Unhandled message: { args [0 ]} ' )
110
+ else :
111
+ logging .exception (event )
96
112
97
113
98
114
@bot .event
@@ -114,7 +130,6 @@ async def on_message(message):
114
130
Lets the bot say happy birthday whenever a user says it
115
131
"""
116
132
117
- print ("Wishing someone a happy birthday!" )
118
133
await message .channel .send ('Happy Birthday! 🎈🎉🎂' )
119
134
120
135
await bot .process_commands (message )
@@ -132,15 +147,11 @@ async def on_message(message):
132
147
for i in range (len (user_message )):
133
148
if (' ' + user_message [i ].lower () in ways_to_say_i_am ):
134
149
135
- print ("Dad joke incoming!" )
136
-
137
150
if len (user_message ) - i < 2 :
138
- print ("False alarm, user message too short!" )
139
151
break
140
152
141
153
else :
142
154
response = "Hi " + " " .join (user_message [i + 1 :]) + "! I'm dad!"
143
- print (response )
144
155
await message .channel .send (response )
145
156
break
146
157
0 commit comments