-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwelcome.py
executable file
·297 lines (207 loc) · 8.44 KB
/
welcome.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# bot.py
import os
import random
import opuslib
import opuslib.api
import opuslib.api.encoder
import opuslib.api.decoder
import glob
import ast
import re
import uuid
import json
import os
from os import path
import discord
import asyncio
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('WELCOME_TOKEN')
PREFIX = os.getenv('WELCOME_PREFIX')
WORKING_DIR = os.getenv('WELCOME_DIR')
AUDIO_DIR = WORKING_DIR + "/audio/"
DB_FILE = WORKING_DIR + "/db.json"
def loadWelcomeMappings():
dictionary = {'active': {}, 'removed': {}}
try:
file = open(DB_FILE, "r")
except OSError:
return dictionary
with file:
contents = file.read()
try:
dictionary = ast.literal_eval(contents)
except SyntaxError:
print("Dictionary syntax error. Overwriting")
file.close()
return dictionary
def dumpDbFile(db):
# Dump the db to the db file
with open(DB_FILE, 'w') as dbFile:
dbFile.write(json.dumps(userMappings))
print("Updated db")
def getActiveTracks(db, userId):
activeTracks = []
if userId in db['active']:
activeTracks = db['active'][userId]
return activeTracks
def getRemovedTracks(db, userId):
removedTracks = []
if userId in db['removed']:
removedTracks = db['removed'][userId]
return removedTracks
async def addUserTrack(db, userid, attachment):
# Save the attachment as a uinque filename
newfilename = str(uuid.uuid4()) + ".mp3"
userspecifiedname = attachment.filename
# Check if the audio directory exists and create it if necessary
if not os.path.exists(AUDIO_DIR):
os.makedirs(AUDIO_DIR)
await attachment.save(AUDIO_DIR + newfilename)
print("Saved audio: {}, {}".format(newfilename, userspecifiedname))
# Add this file to the db for the specified user
if userid not in db['active']:
db['active'][userid] = []
db['active'][userid].append([newfilename, userspecifiedname])
dumpDbFile(db)
def removeUserTrack(db, userId, trackIndex):
if userId not in db['active'] or trackIndex <= 0 or trackIndex > len(db['active'][userId]):
return 0
removedTrack = db['active'][userId][trackIndex - 1]
if userId not in db['removed']:
db['removed'][userId] = []
db['removed'][userId].append(removedTrack)
del db['active'][userId][trackIndex - 1]
dumpDbFile(db)
return 1
def restoreUserTrack(db, userId, trackIndex):
if userId not in db['removed'] or trackIndex <= 0 or trackIndex > len(db['removed'][userId]):
return 0
restoredTrack = db['removed'][userId][trackIndex - 1]
if userId not in db['active']:
db['active'][userId] = []
db['active'][userId].append(restoredTrack)
del db['removed'][userId][trackIndex - 1]
dumpDbFile(db)
return 1
def isValidMention(msg):
match = re.search("<@![0-9]+>", msg)
if match is None:
return 0
return 1
def getIdFromMention(mention):
# Trim off the leading '<@!' and trailing '>' from the mention
return mention[3:len(mention) - 1]
print("Initializing bot with prefix")
print(PREFIX)
# Load the audio mappings from the db file
userMappings = loadWelcomeMappings()
bot = commands.Bot(command_prefix=commands.when_mentioned_or(PREFIX),
description='Welcomes users')
@bot.command(name='welcome', help='Welcomes the specified user with the attached MP3 audio')
async def add_welcome(ctx, usermention):
if not isValidMention(usermention):
await ctx.send("You must @ mention the user who you wish to welcome")
return
if len(ctx.message.attachments) < 1:
await ctx.send("You must attach a .mp3 file")
return
filename, file_extension = os.path.splitext(ctx.message.attachments[0].filename)
if file_extension != '.mp3':
await ctx.send("Attached file is not a .mp3")
return
userid = getIdFromMention(usermention)
print("Requesting welcome for user {}".format(userid))
await addUserTrack(userMappings, userid, ctx.message.attachments[0])
await ctx.send("The user will now be welcomed with your audio")
@bot.command(name='list', help='Lists the welcome clips for the mentioned user')
async def list_welcomes(ctx, userMention):
if not isValidMention(userMention):
await ctx.send("You must @ mention the user who you wish to list")
return
userId = getIdFromMention(userMention)
userFiles = getActiveTracks(userMappings, userId)
if len(userFiles) == 0:
await ctx.send("There are no welcome clips for that user")
return
openedFiles = []
i = 1
for userFile in userFiles:
openedFiles.append(discord.File(AUDIO_DIR + userFile[0], filename="({})-{}".format(i, userFile[1])))
i += 1
await ctx.send("The user has the following welcome clips:", files=openedFiles)
await ctx.send("You may remove a clip by using the command: {}remove @User <Clip Index>".format(PREFIX))
@bot.command(name='remove', help='Removes the specified clip for the given user')
async def remove_clip(ctx, userMention, clipIndexStr):
if not isValidMention(userMention):
await ctx.send("You must @ mention the user who you wish to list")
return
userId = getIdFromMention(userMention)
if not removeUserTrack(userMappings, userId, int(clipIndexStr)):
await ctx.send("Invalid clip index")
return
await ctx.send("The clip has been removed")
@bot.command(name='listRemoved', help='Lists the tracks that have been removed for the mentioned user')
async def list_removed(ctx, userMention):
if not isValidMention(userMention):
await ctx.send("You must @ mention the user who you wish to list")
return
userId = getIdFromMention(userMention)
userFiles = getRemovedTracks(userMappings, userId)
if len(userFiles) == 0:
await ctx.send("There are no removed clips for that user")
return
openedFiles = []
i = 1
for userFile in userFiles:
openedFiles.append(discord.File(AUDIO_DIR + userFile[0], filename="({})-{}".format(i, userFile[1])))
i += 1
await ctx.send("The user has the following removed clips:", files=openedFiles)
await ctx.send("You may restore a clip by using the command: {}restore @User <Clip Index>".format(PREFIX))
@bot.command(name='restore', help='Restores the specified clip for the given user')
async def restore_clip(ctx, userMention, clipIndexStr):
if not isValidMention(userMention):
await ctx.send("You must @ mention the user who you wish to list")
return
userId = getIdFromMention(userMention)
if not restoreUserTrack(userMappings, userId, int(clipIndexStr)):
await ctx.send("Invalid clip index")
return
await ctx.send("The clip has been restored")
@bot.event
async def on_ready():
print('Welcome Logged in as {0} ({0.id})'.format(bot.user))
print('------')
for vc in bot.voice_clients:
await vc.disconnect()
@bot.event
async def on_voice_state_update(member, before, after):
if member.name == "WelcomeBack!" or member.name == "BotDevelopment":
return
channel = after.channel
# Special case for MEE6 bot
if channel is None or before.channel == after.channel or channel.name == "💢 Join to create VC 💢":
return
for vc in bot.voice_clients:
await vc.disconnect()
userFiles = []
if str(member.id) in userMappings['active']:
userFiles = userMappings['active'][str(member.id)]
if len(userFiles) == 0:
print("No audio for user " + member.name)
return
audioChoice = random.choice(userFiles)
voiceClient = await channel.connect()
# Audio choices are stored as a list where the first index is the actual file name in the audio path
audioSource = discord.FFmpegPCMAudio(AUDIO_DIR + audioChoice[0])
def disconnect(error):
coro = voiceClient.disconnect()
fut = asyncio.run_coroutine_threadsafe(coro, bot.loop)
try:
fut.result()
except:
# an error happened sending the message
pass
voiceClient.play(audioSource, after=disconnect)
bot.run(TOKEN)