-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
338 lines (269 loc) · 10.5 KB
/
main.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import discord
from discord.ext import commands
from discord.utils import get
import os
import time
import random
import typing
import logging
import json
import logging.config
############################
# Variables
############################
projectDir = '/home/pi/pipy_bot/'
quotesDir = 'quote'
seperator = '/'
TOKEN = 'TOPSECRET'
BOT_PREFIX = '"'
bot = commands.Bot(command_prefix=BOT_PREFIX)
############################
# Functions
############################
def getRandomList(lowLimit=0, highLimit=10, count=1):
result = []
for i in range(0, count):
sw = False
a = random.randint(lowLimit, highLimit)
if a in result:
sw = True
else:
result.append(a)
while sw:
a = random.randint(lowLimit, highLimit)
if a in result:
pass
else:
sw = False
result.append(a)
return result
def getSubdirs():
subdirs = [x[0] for x in os.walk(projectDir + quotesDir)]
genres = []
for sdir in subdirs:
genres.append(sdir.split(seperator)[-1])
genres.remove(quotesDir)
return genres
def getQuotes(sdir):
#check if exist!!!
listOfQuotes = []
for root, dirs, files in os.walk(projectDir + quotesDir + seperator + sdir):
for filename in files:
listOfQuotes.append(projectDir + quotesDir + seperator + sdir + seperator + filename)
return listOfQuotes
def setup_logging(
default_path=projectDir + 'logging.json',
default_level=logging.INFO,
env_key='LOG_CFG'
):
"""
Setup logging configuration
"""
path = default_path
value = os.getenv(env_key, None)
if value:
path = value
if os.path.exists(path):
with open(path, 'rt') as f:
config = json.load(f)
logging.config.dictConfig(config)
else:
logging.basicConfig(level=default_level)
setup_logging()
logger = logging.getLogger(__name__)
logger.info('Start Yo!')
############################
# bot Start
############################
@bot.event
async def on_ready():
#print('Logged in as: ' + bot.user.name + '\n Simply type "help to get more Information about this awesome Bot')
logger.info('Logged in as: ' + bot.user.name)
@bot.command(name='join', pass_context=True, aliases=['j', 'joi'], help='The Bot will join your Channel!')
async def join(ctx):
logger.info('try to join a channel')
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
"""
await voice.disconnect()
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
print(f"The bot has connected to {channel}\n")
"""
await ctx.send(f"Joined {channel}")
logger.info(f"Joined {channel}")
voice = get(bot.voice_clients, guild=ctx.guild)
voice.play(discord.FFmpegPCMAudio(projectDir + 'start.mp3'), after=lambda e: print("Quote done!"))
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.7
@bot.command(name='leave', pass_context=True, aliases=['l', 'lea'], help='The Bot will leave his Channel :-(')
async def leave(ctx):
logger.info('try to leave a channel')
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.disconnect()
#print(f"The bot has left {channel}")
await ctx.send(f"Left {channel}")
logger.info(f"Left {channel}")
else:
#print("Bot was told to leave voice channel, but was not in one")
await ctx.send("Don't think I am in a voice channel")
logger.info("Don't think I am in a voice channel")
@bot.command(name='play', pass_context=True, aliases=['p', 'pla'], help='Play one Quote. \n You can specify the genre [{}] by typing >> "play [genre] <<'.format(getSubdirs()))
async def play(ctx, genre: typing.Optional[str] = 'random'):
logger.info("Try to play a Quote")
#print(genre)
#getting List of genres
genres = getSubdirs()
#check genre...
if genre.lower() in genres or genre is 'random':
logger.debug('Genre: {}'.format(genre))
#richtige Eingabe --> Zitat ausgeben
if genre is 'random':
genre = genres[random.randint(0, len(genres) - 1)]
logger.debug('Genre random --> {}'.format(genre))
#print(genre)
quotes = getQuotes(genre.lower())
quote = quotes[random.randint(0, len(quotes) - 1)]
await ctx.send("Getting everything ready now 🚀")
voice = get(bot.voice_clients, guild=ctx.guild)
voice.play(discord.FFmpegPCMAudio(quote), after=lambda e: print("Quote done!"))
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.7
#print("playing\n")
else:
await ctx.send("{} gibt es noch nicht! Folgende Genres stehen zu verfügung {}".format(genre, genres))
logger.debug('Wrong genre')
@bot.command(name='play_infinity', pass_context=True, aliases=['pi', 'inf'], help='Playing many Quotes... Actually it will never end! \n You can specify the genre [{}] by typing >> "play_infinity [genre] <<'.format(getSubdirs()))
async def play_infinity(ctx, genre: typing.Optional[str] = 'random'):
logger.info("Try to play infinity Quotes")
sw = False
tmpQuotes = []
def playAgain(count):
counter = count
#print(counter)
if counter % 2 == 0:
logger.debug('counter = {} --> get q quote'.format(counter))
voice = get(bot.voice_clients, guild=ctx.guild)
if len(quotes) == 0:
logger.debug('quotes is empty... lets refill it')
#print('test')
for x in tmpQuotes:
quotes.append(x)
tmpQuotes.clear()
file = quotes[random.randint(0, len(quotes) - 1)]
tmpQuotes.append(file)
quotes.remove(file)
else:
file = quotes[random.randint(0, len(quotes))]
tmpQuotes.append(file)
quotes.remove(file)
#print('ZitatListe länge: {}'.format(len(quotes)))
#print('ZitatListe länge: {}'.format(len(tmpQuotes)))
voice.play(discord.FFmpegPCMAudio(file), after=lambda e: playAgain(counter + 1))
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.7
else:
logger.debug('counter = {} --> get silence'.format(counter))
voice = get(bot.voice_clients, guild=ctx.guild)
file = projectDir + 'silence_030.mp3'
voice.play(discord.FFmpegPCMAudio(file), after=lambda e: playAgain(counter + 1))
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.7
# print(genre)
# getting List of genres
genres = getSubdirs()
# check genre...
if genre.lower() in genres or genre is 'random':
# richtige Eingabe --> Zitat ausgeben
if genre is 'random':
genre = genres[random.randint(0, len(genres) - 1)]
logger.debug('Genre random --> {}'.format(genre))
#print(genre)
quotes = getQuotes(genre.lower())
quote = quotes[random.randint(0, len(quotes) - 1)]
tmpQuotes.append(quote)
quotes.remove(quote)
await ctx.send("Getting everything ready 🚀🚀🚀".format(genre))
voice = get(bot.voice_clients, guild=ctx.guild)
voice.play(discord.FFmpegPCMAudio(quote), after=lambda e: playAgain(1))
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.7
#print("playing\n")
else:
await ctx.send("{} gibt es noch nicht! Folgende Genres stehen zu verfügung {}".format(genre, genres))
logger.debug('Wrong genre')
@bot.command(name='pause', pass_context=True, aliases=['pa', 'pau'], help='Pause current Quote...')
async def pause(ctx):
logger.info('Try to pause')
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_playing():
#print("Music paused")
voice.pause()
await ctx.send("Music paused")
logger.debug('music paused')
else:
#print("Music not playing failed pause")
await ctx.send("Music not playing failed pause")
logger.debug('music not playing')
@bot.command(name='resume', pass_context=True, aliases=['r', 'res'], help='...resume current Quote.')
async def resume(ctx):
logger.info('Try to resume')
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_paused():
#print("Resumed music")
voice.resume()
await ctx.send("Resumed music")
logger.debug('music resumed')
else:
#print("Music is not paused")
await ctx.send("Music is not paused")
logger.debug('music not paused')
@bot.command(name='skip', pass_context=True, aliases=['s', 'sk'], help='Skip current Quote.')
async def skip(ctx):
logger.info('Try to skip')
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_playing():
#print("Playing next title!")
voice.stop()
await ctx.send("Playing next title!")
logger.debug('music skipped')
else:
#print("No music playing failed to skip")
await ctx.send("No music playing failed to skip")
logger.debug('music not playing, failed to skip')
@bot.command(name='stop', pass_context=True, aliases=['st', 'sto'], help='Stop!')
async def stop(ctx):
logger.info('Try to stop')
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_playing():
await voice.disconnect()
logger.debug('music stopped by disconnecting voice')
else:
#print("No music playing failed to Stop")
await ctx.send("No music playing failed to Stop")
logger.debug('No music playing')
"""
@bot.command(pass_context=True)
async def hlp(ctx):
await ctx.send('''🚀🚀🚀 STROMBERG ZITATE 🚀🚀🚀
\n Starte Den Befehl mit einem >> " <<
\n join --> Der Bot kommt zu dir in den Sprachchannel
\n leave --> Der Bot verlässt den Sprachchannel
\n play --> Ein Zitat wird abgespielt
\n inf --> Alle Zitate werden abgespielt
\n skip --> Nächstes Zitat
\n pause --> Pausieren
\n resume --> Weiter gehts''')
"""
############################
# bot End
############################
bot.run(TOKEN)