diff --git a/kabot/kabot/kabot.py b/kabot/kabot/kabot.py index 4e8582c..3199bdd 100644 --- a/kabot/kabot/kabot.py +++ b/kabot/kabot/kabot.py @@ -27,119 +27,25 @@ class Mybot(commands.Cog): self.sounds_history = [] self.play_next.start() - @commands.command() - async def join(self, ctx): - channel = [x for x in self.bot.get_all_channels() if x.name == "Général"][0] - await channel.connect() - await asyncio.sleep(2) + @tasks.loop(seconds=1.5) + async def play_next(self): + if self.sounds: + audio_file, ctx = self.sounds[0] + source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(audio_file)) + if not ctx.voice_client.is_playing() and ctx.voice_client.is_connected(): + ctx.voice_client.play(source, after=lambda e: print('Player error: %s' % e) if e else None) + self.sounds.pop(0) + self.sounds_history.reverse() + self.sounds_history.append((audio_file, ctx)) + self.sounds_history.reverse() + if len(self.sounds_history) > 5: + self.sounds_history = self.sounds_history[:5] + #Les commandes interagir avec le bot. @commands.command() async def disconnect(self, ctx): await ctx.guild.voice_client.disconnect() - @commands.guild_only() - @commands.command() - async def joke(self, ctx, folder=None): - user = ctx.message.author.name - if not folder or not ctx.message.content: - audio_file = random.choice([f for f in Path(here + '/Audio/').glob('**/*.mp3')]) - else: - folder = folder.lower() - audio_file = random.choice([f for f in Path(here + '/Audio/').glob('**/*.mp3') if folder in str(f).lower()]) - self.sounds.append((audio_file,ctx)) - - @commands.command() - async def repeat(self, ctx): - #if ctx.message.author.name == self.bot.user.name: - # return - user = ctx.message.author.name - source = self.sounds_history[0] - self.sounds.append(source) - - @commands.command(help='Gif me') - async def gif(self, ctx): - query = ctx.message.content.replace('!gif ','') - print(query) - api_instance = giphy_client.DefaultApi() - api_key = "udhL9Rgdme3HpAoFhWVZoYM0l0XwkzLz" - lang = 'fr' - try: - api_response = api_instance.gifs_search_get(api_key, query, lang=lang, limit=10) - api_response.to_dict()['data'][0] - get_url = random.choice(api_response.to_dict()['data']) - get_url['url'] - await ctx.channel.send(get_url['url']) - except ApiException as e: - await ctx.channel.send("Exception when calling DefaultApi->gifs_search_get: %s\n" % e) - - @commands.command() - async def welcome(self, ctx): - #if ctx.message.author.name == self.bot.user.name: - # return - user = ctx.message.author.name - print(user) - try: - audio_file = random.choice([f for f in os.listdir(here + '/Audio/%s/' % user) if f.endswith('.mp3')]) - audio_file = here + '/Audio/%s/' % user + audio_file - except: - audio_file = random.choice([f for f in Path(here + '/Audio/').glob('**/*.mp3')]) - self.sounds.append((audio_file,ctx)) - - @commands.command(help='Update local repo') - async def update(self, message): - output = Popen('git pull'.split(), stdout=PIPE).communicate()[0] - await message.channel.send(output.decode('utf-8')) - - @commands.command(help='Restart Bot') - async def restart(self, ctx): - await self.bot.logout() - - @commands.command(help='Restart Bot') - async def restart_queue(self, ctx): - self.play_next.restart() - - @commands.command(help='check if bot always online') - async def ping(self, message): - await message.channel.send('pong') - - @commands.command(help='count lines numbers in quote file') - async def lines(self, ctx): - path = here + '/' + ctx.channel - with open(path, 'r') as f: - lines = f.read().splitlines() - nb_lines = len(lines) - with ctx.channel.typing(): - await asyncio.sleep(0.5) - await ctx.channel.send("j'ai %s lignes dans mon stock" % nb_lines) - - @commands.command(help='slap this ass') - async def slap(self, ctx, user=None): - slap_multiple = [ - "%s prend un coup de pied au cul", - "Descente du coude sur %s", - "%s est propulsé par dessus la TROISIEME CORDE!", - "Le mec en rose, c'est moi et le mec en jaune c'est %s! https://giphy.com/gifs/gSIz6gGLhguOY", - ] - if not user or not ctx.message.mentions: - online_members = [] - members = ctx.guild.members - for member in members: - if str(member.status) == "online": - online_members.append(member) - user = random.choice(online_members) - user = user.mention - elif ctx.message.mentions: - user = ctx.message.mentions[0] - user = user.mention - if user == self.bot.user.mention: - with ctx.channel.typing(): - await asyncio.sleep(0.5) - await ctx.channel.send("je tribuche par terre et je sais pas comment") - else: - with ctx.channel.typing(): - await asyncio.sleep(len(slap_multiple)/4) - await ctx.channel.send(random.choice(slap_multiple) % user) - @commands.command(help="Interrogation issues \n Args: list, search[mot clé] et add[nom de l'issue]") async def issue(self, ctx, *args): if args: @@ -171,6 +77,102 @@ class Mybot(commands.Cog): else: await ctx.channel.send('unknown command') + @commands.command() + async def join(self, ctx): + channel = [x for x in self.bot.get_all_channels() if x.name == "Général"][0] + await channel.connect() + await asyncio.sleep(2) + + @commands.command(help='count lines numbers in quote file') + async def lines(self, ctx): + path = here + '/' + ctx.channel + with open(path, 'r') as f: + lines = f.read().splitlines() + nb_lines = len(lines) + with ctx.channel.typing(): + await asyncio.sleep(0.5) + await ctx.channel.send("j'ai %s lignes dans mon stock" % nb_lines) + + @commands.command(help='check if bot always online') + async def ping(self, message): + await message.channel.send('pong') + + @commands.command() + async def repeat(self, ctx): + # if ctx.message.author.name == self.bot.user.name: + # return + user = ctx.message.author.name + source = self.sounds_history[0] + self.sounds.append(source) + + @commands.command(help='Restart Bot') + async def restart(self, ctx): + await self.bot.logout() + + @commands.command(help='Restart Bot') + async def restart_queue(self, ctx): + self.play_next.restart() + + @commands.command(help='Update local repo') + async def update(self, message): + output = Popen('git pull'.split(), stdout=PIPE).communicate()[0] + await message.channel.send(output.decode('utf-8')) + + #Les commandes pour faire mumuse avec le bot. + @commands.command(help="randomsur l'avenir des gens.") + async def avenir(self, ctx): + + list_mot = ("tu seras curé, tu t'occuperas plus spécialement du catéchisme. ", + "tu seras animateur de soirées pour les gays pride. ", + "tu seras gynecologue dans une maison de retraite.", + "tu iras vivre en thaïland à cause de ton job. car tu seras ladyboy dans un bar.", + "tu sera DSI chez jacky et Michel", + "tu seras arroseur de plante aquatique.") + choix = random.choice(list_mot) + with ctx.channel.typing(): + await asyncio.sleep(len(choix) / 4) + await ctx.channel.send(choix) + + @commands.command(help="optionnal args : ") + async def contrepeterie(self, ctx, *args): + response = None + path = here + '/ressources/contrepeteries.txt' + lines = open(path).read().splitlines() + myline = random.choice(lines) + + try: + response = "%s" % (myline) + except: + response = "Unknow error, try: !contrepeterie [mot clef]" + await ctx.send(response) + + @commands.command(help='Gif me') + async def gif(self, ctx): + query = ctx.message.content.replace('!gif ', '') + print(query) + api_instance = giphy_client.DefaultApi() + api_key = "udhL9Rgdme3HpAoFhWVZoYM0l0XwkzLz" + lang = 'fr' + try: + api_response = api_instance.gifs_search_get(api_key, query, lang=lang, limit=10) + api_response.to_dict()['data'][0] + get_url = random.choice(api_response.to_dict()['data']) + get_url['url'] + await ctx.channel.send(get_url['url']) + except ApiException as e: + await ctx.channel.send("Exception when calling DefaultApi->gifs_search_get: %s\n" % e) + + @commands.guild_only() + @commands.command() + async def joke(self, ctx, folder=None): + user = ctx.message.author.name + if not folder or not ctx.message.content: + audio_file = random.choice([f for f in Path(here + '/Audio/').glob('**/*.mp3')]) + else: + folder = folder.lower() + audio_file = random.choice([f for f in Path(here + '/Audio/').glob('**/*.mp3') if folder in str(f).lower()]) + self.sounds.append((audio_file, ctx)) + @commands.command(help="optionnal args : [livre] [character]") async def kaamelott(self, ctx, *args): response = None @@ -188,7 +190,7 @@ class Mybot(commands.Cog): if args: perso = ' '.join(args) if perso and livre: - url = 'https://kaamelott.chaudie.re/api/random/livre/%s/personnage/%s' % (livre,perso) + url = 'https://kaamelott.chaudie.re/api/random/livre/%s/personnage/%s' % (livre, perso) elif perso: url = 'https://kaamelott.chaudie.re/api/random/personnage/%s' % perso else: @@ -200,19 +202,6 @@ class Mybot(commands.Cog): response = "Unknow error, try: !kaamelott [livre] [character]" await ctx.send(response) - @commands.command(help="optionnal args : ") - async def contrepeterie(self, ctx, *args): - response = None - path = here + '/ressources/contrepeteries.txt' - lines = open(path).read().splitlines() - myline =random.choice(lines) - - try: - response = "%s" % (myline) - except: - response = "Unknow error, try: !contrepeterie [mot clef]" - await ctx.send(response) - @commands.command(help="Je menotte une cornemuse et je fume Eddy Malou") async def kamoulox(self, ctx): sans_verbe = get_word('nom').text + " " + get_word('complement').get('m') + " et " + get_word('nom').text + " " + get_word('complement').get('m') + "." @@ -304,39 +293,49 @@ class Mybot(commands.Cog): channel = [x for x in self.bot.get_all_channels() if x.name == "général"][0] await channel.send(sentence) + @commands.command(help='slap this ass') + async def slap(self, ctx, user=None): + slap_multiple = [ + "%s prend un coup de pied au cul", + "Descente du coude sur %s", + "%s est propulsé par dessus la TROISIEME CORDE!", + "Le mec en rose, c'est moi et le mec en jaune c'est %s! https://giphy.com/gifs/gSIz6gGLhguOY", + ] + if not user or not ctx.message.mentions: + online_members = [] + members = ctx.guild.members + for member in members: + if str(member.status) == "online": + online_members.append(member) + user = random.choice(online_members) + user = user.mention + elif ctx.message.mentions: + user = ctx.message.mentions[0] + user = user.mention + if user == self.bot.user.mention: + with ctx.channel.typing(): + await asyncio.sleep(0.5) + await ctx.channel.send("je tribuche par terre et je sais pas comment") + else: + with ctx.channel.typing(): + await asyncio.sleep(len(slap_multiple) / 4) + await ctx.channel.send(random.choice(slap_multiple) % user) - @tasks.loop(seconds=1.5) - async def play_next(self): - if self.sounds: - audio_file,ctx = self.sounds[0] - source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(audio_file)) - if not ctx.voice_client.is_playing() and ctx.voice_client.is_connected(): - ctx.voice_client.play(source, after=lambda e: print('Player error: %s' % e) if e else None) - self.sounds.pop(0) - self.sounds_history.reverse() - self.sounds_history.append((audio_file,ctx)) - self.sounds_history.reverse() - if len(self.sounds_history) >5: - self.sounds_history = self.sounds_history[:5] - - @commands.command(help= "randomsur l'avenir des gens.") - async def avenir(self, ctx): - - list_mot = ("tu seras curé, tu t'occuperas plus spécialement du catéchisme. ", - "tu seras animateur de soirées pour les gays pride. ", - "tu seras gynecologue dans une maison de retraite.", - "tu iras vivre en thaïland à cause de ton job. car tu seras ladyboy dans un bar.", - "tu sera DSI chez jacky et Michel", - "tu seras arroseur de plante aquatique.") - choix = random.choice(list_mot) - with ctx.channel.typing(): - await asyncio.sleep(len(choix)/4) - await ctx.channel.send(choix) - - - + @commands.command() + async def welcome(self, ctx): + # if ctx.message.author.name == self.bot.user.name: + # return + user = ctx.message.author.name + print(user) + try: + audio_file = random.choice([f for f in os.listdir(here + '/Audio/%s/' % user) if f.endswith('.mp3')]) + audio_file = here + '/Audio/%s/' % user + audio_file + except: + audio_file = random.choice([f for f in Path(here + '/Audio/').glob('**/*.mp3')]) + self.sounds.append((audio_file, ctx)) def get_word(word_type): + """Chercher les mots pour la fonction kamoulox dans le fichier xml""" content = [] with open(here + "/ressources/base_kml.xml", "r", encoding="ISO-8859-1") as file: content = file.readlines() @@ -398,6 +397,7 @@ def get_word(word_type): result = 'Nique bien ta mère!' return result +#Le do[main]e du bot. def main(): token = "NjI3MTM3NDY1MDA5ODMxOTQ2.XY4Raw.pw8sAen3bNR5aYsoTChQOudM0L8" bot = commands.Bot(command_prefix='!') @@ -406,6 +406,44 @@ def main(): if "test" in argv[-1]: test=True + @aiocron.crontab('50 9-23/1 * * *') + async def kron(): + kron_choice = random.choice(["kaamelott", "slap", "kamoulox", False, False, False]) + if kron_choice: + await asyncio.sleep(random.choice(range(3550))) + for channel in bot.get_all_channels(): + if channel.name == "général": + current_chan = channel + ctx = await bot.get_context(current_chan.last_message) + ctx.message.content = "" + print(ctx.message.content) + random_cmd = bot.get_command(kron_choice) + await random_cmd.invoke(ctx) + + @bot.event + async def on_message(message): + print(message.content) + if message.author == bot.user: + return + else: + if bot.user in message.mentions \ + and len(message.mentions) < 3 \ + and len(message.content.splitlines()) == 1: + path = '/data/log/%s.log' % message.channel + with open(path, 'r') as f: + lines = f.read().splitlines() + if not message.content in lines: + with open(path, 'a') as f: + f.write(message.content + '\n') + response = random.choice(lines).replace(str(bot.user.id), str(message.author.id)) + with message.channel.typing(): + if "http" in response: + await asyncio.sleep(len(response) / 5) + else: + await asyncio.sleep(len(response) / 4) + await message.channel.send(response) + await bot.process_commands(message) + @bot.event async def on_ready(): print('yeah baby!') @@ -423,19 +461,17 @@ def main(): for member in channel.members: await member.edit(mute=False) - @aiocron.crontab('50 9-23/1 * * *') - async def kron(): - kron_choice = random.choice(["kaamelott","slap","kamoulox",False, False, False]) - if kron_choice: - await asyncio.sleep(random.choice(range(3550))) + @bot.event + async def on_voice_state_update(member, before, after): + if before.channel is None and after.channel: for channel in bot.get_all_channels(): if channel.name == "général": current_chan = channel ctx = await bot.get_context(current_chan.last_message) - ctx.message.content = "" - print(ctx.message.content) - random_cmd = bot.get_command(kron_choice) - await random_cmd.invoke(ctx) + ctx.message.author = member + welcome = bot.get_command('welcome') + await asyncio.sleep(2) + await welcome.invoke(ctx) @aiocron.crontab('*/5 * * * *') async def pipelette(): @@ -452,42 +488,6 @@ def main(): joke = bot.get_command('joke') await joke.invoke(ctx) - @bot.event - async def on_message(message): - print(message.content) - if message.author == bot.user: - return - else: - if bot.user in message.mentions \ - and len(message.mentions) < 3 \ - and len(message.content.splitlines()) == 1: - path = '/data/log/%s.log' % message.channel - with open(path, 'r') as f : - lines = f.read().splitlines() - if not message.content in lines: - with open(path, 'a') as f : - f.write(message.content + '\n') - response = random.choice(lines).replace(str(bot.user.id), str(message.author.id)) - with message.channel.typing(): - if "http" in response: - await asyncio.sleep(len(response)/5) - else: - await asyncio.sleep(len(response)/4) - await message.channel.send(response) - await bot.process_commands(message) - - @bot.event - async def on_voice_state_update(member,before,after): - if before.channel is None and after.channel: - for channel in bot.get_all_channels(): - if channel.name == "général": - current_chan = channel - ctx = await bot.get_context(current_chan.last_message) - ctx.message.author = member - welcome = bot.get_command('welcome') - await asyncio.sleep(2) - await welcome.invoke(ctx) - bot.add_cog(Mybot(bot)) bot.run(token)