Discord机器人仅对命令做出一次反应

时间:2020-11-06 20:55:41

标签: python discord discord.py praw

使用G!GameDeals命令时,Discord机器人的功能之一是从subreddit GameDeals读取,过滤和发送新帖子。问题是,如果在使用该命令后该机器人没有响应,则该机器人只会执行一次。如果我重新启动漫游器,该命令将再次运行一次。

import discord
import datetime
from discord.ext import commands
import asyncio
import praw
import audioread

TOKEN = "___"
bot = commands.Bot(command_prefix="G!")
client = discord.Client()
split_1 = "I'm"
split_2 = "i'm"
split_3 = "Im"
split_4 = "im"
rest = None
reddit = praw.Reddit(client_id='___', client_secret='___',
                     username='____', password='___', user_agent='test')

subreddit = reddit.subreddit('GameDeals')
new_GameDeals = subreddit.new(limit=50)
aanuit = ''


@bot.command(name='tutturu')
async def tutturu(ctx):
    path_mp3 = "Tutturuu.mp3"
    path_ffmpeg = r"C:\ffmpeg\bin\ffmpeg.exe"
    voice_state = ctx.author.voice
    if voice_state is not None:
        voice_channel = ctx.author.voice.channel
        if voice_channel is not None:
            vc = await voice_channel.connect()
            vc.play(discord.FFmpegPCMAudio(executable=path_ffmpeg, source=path_mp3))
            with audioread.audio_open(path_mp3) as f:
                await asyncio.sleep(f.duration)
            await vc.disconnect()
        await ctx.message.delete()
    else:
        await ctx.channel.send(str(ctx.author.name) + " is not in a channel.")



@bot.command(name='GameDeals')
async def GameDeals(ctx):
    for post in new_GameDeals:
        if '[STEAM]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)
        elif '[Steam]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)
        elif '[Epic Games]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)
        elif '[Uplay]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)
        elif '[Humble Bundle]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)
        elif '[Epic]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)
        elif '[UPLAY]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)


@bot.command(name='Help')
async def Help(ctx):
    await ctx.message.channel.send('1) G!tutturu speelt tutturu af')
    await ctx.message.channel.send('2) G!GameDeals toont je de gratis game deals van het moment')
    await ctx.message.channel.send('3) G!turnoff aan zet de tutturu aan')
    await ctx.message.channel.send('3) G!turnoff uit zet de tutturu af')


@bot.event
async def on_message(message):
    if 'Creeper' in message.content or 'creeper' in message.content and not message.author.bot:
        await message.channel.send('OW MAN')
    if 'ah fuck' in message.content or 'Ah fuck' in message.content and not message.author.bot:
        await message.channel.send("I can't believe you've done this")
    if "Who didn't flush the toilet when they took a shat" in message.content and not message.author.bot:
        await message.channel.send('DISGUUUSTAAAN!!!!!')
        await message.channel.send(file=discord.File('disgustang.png'))
    if '❤' in message.content and not message.author.bot:
        await message.channel.send('❤')
    if message.content == 'koekoek' or message.content == 'Koekoek' and not message.author.bot:
        await message.channel.send(datetime.datetime.now())
    if "I'm" in message.content and not message.author.bot:
        rest = message.content.partition(split_1)[2]
        await message.channel.send('Hello' + rest + ", I'm dad")
    if "i'm" in message.content and not message.author.bot:
        rest = message.content.partition(split_2)[2]
        await message.channel.send('Hello' + rest + ", I'm dad")
    if "Im" in message.content and not message.author.bot:
        rest = message.content.partition(split_3)[2]
        await message.channel.send('Hello' + rest + ", I'm dad")
    if "im" in message.content and not message.author.bot:
        rest = message.content.partition(split_4)[2]
        await message.channel.send('Hello' + rest + ", I'm dad")
    if 'fire' in message.content or 'Fire' in message.content or 'lit' in message.content or 'Lit' in message.content and not message.author.bot:
        await message.channel.send('?')
    if 'geil' in message.content or 'Geil' in message.content or 'lekker' in message.content or 'Lekker' in message.content and not message.author.bot:
        await message.channel.send('?')
    await bot.process_commands(message)


@bot.command(name='turnoff')
async def turnoff(ctx):
    global aanuit
    uitkomst = ctx.message.content.partition('turnoff')[2]
    if uitkomst == ' uit':
        aanuit = 'uit'
    elif uitkomst == ' aan':
        aanuit = 'aan'


@bot.event
async def on_voice_state_update(member: discord.Member, before, after):
    vc_before = before.channel
    vc_after = after.channel
    path_mp3 = "Tutturuu.mp3"
    path_ffmpeg = r"C:\ffmpeg\bin\ffmpeg.exe"
    global aanuit
    for role in member.roles:
        if role.name == "Gerbinbot 3000":
            return
    if vc_before == vc_after:
        return
    if vc_before is None and aanuit == 'aan':
        channel = member.voice.channel
        vc = await channel.connect()
        await asyncio.sleep(.5)
        vc.play(discord.FFmpegPCMAudio(executable=path_ffmpeg, source=path_mp3))
        with audioread.audio_open(path_mp3) as f:
            await asyncio.sleep(f.duration)
        await vc.disconnect()
    elif vc_after is None:
        return
    elif vc_after is not None and vc_before is not None and aanuit == 'aan':
        channel = member.voice.channel
        vc = await channel.connect()
        await asyncio.sleep(.5)
        vc.play(discord.FFmpegPCMAudio(executable=path_ffmpeg, source=path_mp3))
        with audioread.audio_open(path_mp3) as f:
            await asyncio.sleep(f.duration)
        await vc.disconnect()
bot.run(TOKEN)

0 个答案:

没有答案