5
5
"""
6
6
7
7
8
- #################################
9
- #
10
- import os #
11
- import discord #
12
- from dotenv import load_dotenv #
13
- import random #
14
- #
15
- #################################
16
-
17
-
18
- load_dotenv () # Loads .env file
19
- TOKEN = os .getenv ('DISCORD_TOKEN' ) # Tells the program to use the token provided in .env
20
- client = discord .Client () # Tells the program to use the default Client class
21
-
22
-
23
- @client .event # Logs a sucessful connection to the Discord server
8
+ #####################################
9
+ #
10
+ import os #
11
+ import discord #
12
+ from dotenv import load_dotenv #
13
+ import random #
14
+ from discord .ext import commands #
15
+ #
16
+ #####################################
17
+
18
+ #########################################
19
+ #
20
+ bot = commands .Bot (command_prefix = '!' ) #
21
+ TOKEN = os .getenv ('DISCORD_TOKEN' ) #
22
+ VERSION = "a0.1" #
23
+ ACTIVITY = discord .Game ("!help" ) #
24
+ #
25
+ #########################################
26
+
27
+ load_dotenv ()
28
+
29
+
30
+ @bot .event
24
31
async def on_ready ():
25
- print (f'{ client .user .name } has sucessfully connected to the server!' )
32
+ """
33
+ Logs a message with bot name and version when the bot starts. Sets the
34
+ bot activity to ACTIVITY.
35
+ """
36
+
37
+ print (f'{ bot .user .name } { VERSION } has sucessfully connected to Discord.' )
38
+ await bot .change_presence (activity = ACTIVITY )
39
+
26
40
27
41
28
- @client .event # Responds to a new member joining the server
42
+ @bot .event
29
43
async def on_member_join (member ):
30
- await member .create_dm () # Creates a DM with the user and sends a message
44
+ """
45
+ Direct-messages a user whenever the join the server
46
+ """
47
+
48
+ await member .create_dm ()
31
49
await member .dm_channel .send (
32
50
f"Hi { member .name } , welcome to Konnor's Discord server. Please set your "
33
51
"nickname to match the naming scheme used on the server. For example, if "
@@ -36,11 +54,75 @@ async def on_member_join(member):
36
54
)
37
55
38
56
39
- @client .event # Responds to a user sending a message in the server
57
+ @bot .event
58
+ async def on_error (event , * args , ** kwargs ):
59
+ """
60
+ Writes to err.log whenever a message triggers an error
61
+ """
62
+
63
+ with open ('err.log' , 'a' , encoding = 'utf-8' ) as errfile :
64
+ if event == 'on_message' :
65
+ errfile .write (f'Unhandled message: { args [0 ]} \n ' )
66
+ else :
67
+ raise
68
+
69
+
70
+ @bot .event
40
71
async def on_message (message ):
41
- if message .author == client .user :
42
- return # Ensures ProtoBot will not respond to its own messages
43
-
72
+ """
73
+ Allows the bot to respond to user messages rather than commands
74
+ """
75
+
76
+ if message .author == bot .user :
77
+ """
78
+ Tells the bot to ignore its own messages
79
+ """
80
+
81
+ return
82
+
83
+ if 'im' in message .content .lower () or 'i\' m' in message .content .lower () or 'i am' in message .content .lower ():
84
+ """
85
+ Lets the bot tell the famous "Hi x! I'm dad!" joke
86
+ """
87
+
88
+ user_message = message .content .split ()
89
+ ways_to_say_i_am = ["im" , "i'm" , "i am" ]
90
+ print ("Dad joke incoming!" )
91
+
92
+ for i in range (len (user_message )):
93
+ if len (user_message ) - i < 2 :
94
+ print ("False alarm, user message too short!" )
95
+ break
96
+
97
+ elif user_message [i ].lower () in ways_to_say_i_am :
98
+ response = "Hi " + " " .join (user_message [i + 1 :]) + "! I'm dad!"
99
+ print (response )
100
+ await message .channel .send (response )
101
+ break
102
+
103
+ elif len (user_message ) - i < 3 :
104
+ print ("False alarm, user message too short!" )
105
+ break
106
+
107
+ elif user_message [i ].lower () + " " + user_message [i + 1 ].lower () in ways_to_say_i_am :
108
+ response = "Hi " + " " .join (user_message [i + 2 :]) + "! I'm dad!"
109
+ print (response )
110
+ await message .channel .send (response )
111
+ break
112
+
113
+ elif 'happy birthday' in message .content .lower ():
114
+ """
115
+ Lets the bot say happy birthday whenever a user says it
116
+ """
117
+
118
+ print ("Wishing someone a happy birthday!" )
119
+ await message .channel .send ('Happy Birthday! 🎈🎉🎂' )
120
+
121
+ await bot .process_commands (message )
122
+
123
+
124
+ @bot .command (name = '99' , help = 'Responds with a random Brooklyn 99 quote.' )
125
+ async def nine_nine (ctx ):
44
126
b99_quotes = [
45
127
"I'm the human form of the 💯 emoji." ,
46
128
"Bingpot!" ,
@@ -58,29 +140,13 @@ async def on_message(message):
58
140
"But if you’re here, who’s guarding Hades?"
59
141
]
60
142
61
- ways_to_say_i_am = ["im" , "i'm" , "i am" ]
62
-
63
- if message .content == '99!' :
64
- response = random .choice (b99_quotes )
65
- await message .channel .send (response ) # Sends a Brooklyn 99 quote whenever a user says '99!'
66
-
67
- elif 'i\' m ' or 'im ' or 'i am ' in message .content .lower ():
68
- user_message = message .content .split ()
69
- for i in range (len (user_message )):
70
- if len (user_message ) - i < 2 :
71
- break
72
-
73
- elif user_message [i ].lower () in ways_to_say_i_am :
74
- await message .channel .send ("Hi " + " " .join (user_message [i + 1 :]) + "! I'm dad!" )
75
- break
143
+ response = random .choice (b99_quotes )
144
+ await ctx .send (response )
76
145
77
- elif len (user_message ) - i < 3 :
78
- break
79
-
80
- elif user_message [i ].lower () + " " + user_message [i + 1 ].lower () in ways_to_say_i_am :
81
- await message .channel .send ("Hi " + " " .join (user_message [i + 2 :]) + "! I'm dad!" )
82
- break
83
-
84
146
147
+ @bot .command (name = 'raise-exception' , help = 'Logs an exception with message data. Developer use.' )
148
+ async def raise_exception (ctx ):
149
+ await ctx .send ('Raising an exception to my console...' )
150
+ raise discord .DiscordException
85
151
86
- client .run (TOKEN )
152
+ bot .run (TOKEN )
0 commit comments