-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
180 lines (145 loc) · 6.19 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
import random
import complaints
from glossary import Glossary
import encoder
# Getting parameters from .env file
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
W_CHANNEL = os.getenv('WELCOME_CHANNEL')
core_path = os.getenv('CORE_PATH')
# Specifying permissions bot has. Default + members + messages
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
# Specifying the symbol bot uses for commands
bot = commands.Bot(command_prefix='!', intents=intents)
# The message will be printed to the console if bot has successfully connected to discord
@bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
# The welcome message printed by the bot when a new user joins. You can change the content of welcome_message to make
# your own custom message
@bot.event
async def on_member_join(member):
welcome_channel = bot.get_channel(int(W_CHANNEL))
if welcome_channel:
welcome_message = (
f'Hi @{member.name}, welcome to ASS (Advanced Security Society)! \n\n'
'We are trying here build a supporting environment '
'for like-minded people on the course, who are really interested '
'in the course. We discuss stuff, ask questions, help each other, etc. '
'\n\nThere are some pretty simple rules: use your **real name** as a server nickname, '
'do not insult or bully people, and stick to the server purpose. If you have '
'some questions regarding studying, ask them in #question-channel :)\n\n'
'Please, feel free to say "Hi!" in #speak-here'
)
await welcome_channel.send(welcome_message)
# Fun command that chooses a random user from members and calls them a random good word from the word_list
@bot.command(name='encourage', help='Says good stuff about a random member of a server')
async def nine_nine(ctx):
guild = ctx.guild
members = guild.members
member_list = [member.name for member in members]
word_list = [
'clever', 'smart', 'intelligent', 'bright', 'sharp', 'astute', 'ingenious', 'resourceful', 'knowledgeable',
'quick-witted', 'shrewd', 'brainy', 'crafty', 'cunning', 'wise', 'savvy', 'sharp-witted', 'apt', 'adroit',
'perceptive'
]
name = random.choice(member_list)
word = random.choice(word_list)
response = f'{name} is {word}'
await ctx.send(response)
# The following function is used to add a complaint to a server's complaints file
# It checks if the server's complaints directory exists and if not - creates one
# Then it takes user's nickname and uses it as a key for the complaints' dictionary
# If user id is already a key in dictionary, it adds 1 to it.
# So if user adds a lot of complaints, their keys would look like 'userid', 'userid1', 'userid2', ...
@bot.command(name='cmp', help='Files a complaint to the bot')
async def add_complaint(ctx, *args):
guild = ctx.guild
author = ctx.message.author.name
message = ' '.join(args)
dir_path = core_path + str(guild)
file_path = dir_path + "/complaints.txt"
if not os.path.isdir(dir_path):
os.mkdir(core_path + str(guild))
db = complaints.ComplaintsDb()
await db.load(file_path)
key = str(author)
start_length = len(key)
existent_keys = db.get_keys()
while key in existent_keys:
if len(key) == start_length:
key = key + '1'
else:
extra = int(key[start_length:])
extra += 1
key = key[:start_length] + str(extra)
db.add_complaint(key, message)
db.save()
await ctx.send('Complaint added with key = ' + str(key) + '; Text = ' + message)
# Printing all complaints to the channel where command was called
@bot.command(name='cmp-all', help='Prints all complaints')
async def get_complaints(ctx):
guild = ctx.guild
dir_path = core_path + str(guild)
file_path = dir_path + "/complaints.txt"
if not os.path.isdir(dir_path):
os.mkdir(core_path + str(guild))
db = complaints.ComplaintsDb()
await db.load(file_path)
bot_response = '**Here is the list of complaints**: \n\n' + '\n\n'.join(db.get_all_complaints())
await ctx.send(bot_response)
# Printing a single complaint to the channel where the command was called. User needs to specify id
# after !cmp-get. Example: !cmp-get {complaint_id}
@bot.command(name='cmp-get', help='Gets a complaint by complaint-id')
async def get_complaint(ctx, arg1):
guild = ctx.guild
dir_path = core_path + str(guild)
file_path = dir_path + "/complaints.txt"
if not os.path.isdir(dir_path):
os.mkdir(core_path + str(guild))
db = complaints.ComplaintsDb()
await db.load(file_path)
if arg1 in db.get_keys():
await ctx.send(db.get_complaint(arg1))
else:
await ctx.send('No complaint found')
# Using Glossary.py for obtaining definitions of the given word
@bot.command(name='def', help='Gets definitions of the word')
async def get_complaint(ctx, arg1):
rules = Glossary.Rules()
rules.set_limit_of_descriptions(3)
gl = Glossary()
word = gl.Word(arg1, rules)
output_message = '**' + arg1 + '**' + ': \n'
if word.exists():
for part_of_speech in word.get_parts_of_speech():
output_message = output_message + "--------" + part_of_speech + "--------\n"
for definition in word.get_definitions_of_part_of_speech(part_of_speech):
output_message = output_message + definition + '\n'
else:
output_message += "Couldn't find this word :("
await ctx.send(output_message)
@bot.command(name='b64e', help='base64 encoding')
async def b64e(ctx, *args):
text = ''.join(args)
await ctx.send(encoder.b64_e(text))
@bot.command(name='b64d', help='base64 decoding')
async def b64e(ctx, *args):
text = ''.join(args)
await ctx.send(encoder.b64_d(text))
@bot.command(name='urle', help='url encoding')
async def b64e(ctx, *args):
text = ''.join(args)
await ctx.send(encoder.url_e(text))
@bot.command(name='urld', help='url decoding')
async def b64e(ctx, *args):
text = ''.join(args)
await ctx.send(encoder.url_d(text))
# Running the bot
bot.run(TOKEN)