让Python Discord bot读取不同的反应

2024-04-24 13:20:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用对消息的反应制作一个简单的石头剪纸python discord机器人。我研究并改编了下面的一些代码,并尝试使用它,但我意识到,只有当你先用纸,然后用石头回应消息时,它才会起作用(顺便说一句,没有剪刀,因为我意识到这是个问题)

import discord
import random
import asyncio

TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))


@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = str(message.content)
    channel = str(message.channel.name)
    print(f'{username}: {user_message} ({channel})')
    random_choice = 0

    if message.author == client.user:
        return

    if user_message.lower() == 'p!rps':
        channel = message.channel
        await channel.send('Welcome to Rock Paper Scissors. Please react to this message with a rock, paper (📰), or scissors emoji to play the game!')

        def check(reaction, user):
            return user == message.author and str(reaction.emoji) == '📰'

        try:
            reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
        except asyncio.TimeoutError:
            await channel.send('Timed out. Pleas re-enter command to play again.')
        else:
            random_choice = random.randint(1, 3)
            if random_choice == 1:
                await channel.send('You choose paper. I choose rock.')
                await channel.send('You win!')
            if random_choice == 2:
                await channel.send('You choose paper. I choose paper.')
                await channel.send('We tied!')
            if random_choice == 3:
                await channel.send('You choose paper. I choose scissors.')
                await channel.send('I win!')
            
        def check(reaction, user):
            return user == message.author and str(reaction.emoji) == '🪨'

        try:
            reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
        except asyncio.TimeoutError:
            await channel.send('Timed out. Pleas re-enter command to play again.')
        else:
            random_choice = random.randint(1, 3)
            if random_choice == 1:
                await channel.send('You choose rock. I choose rock.')
                await channel.send('We tied!')
            if random_choice == 2:
                await channel.send('You choose rock. I choose paper.')
                await channel.send('I win!')
            if random_choice == 3:
                await channel.send('You choose rock. I choose scissors.')
                await channel.send('You win!')
            

                return



client.run(TOKEN)

有人能帮我修一下密码吗


Tags: clientyousendmessageifcheckchannelrandom
1条回答
网友
1楼 · 发布于 2024-04-24 13:20:17

您可以在支票中使用return user == message.author and str(reaction.emoji) in ('📰', '🧱', '✂️')

编辑:使用您的代码,应该如下所示:

    if user_message.lower() == 'p!rps':
        channel = message.channel
        await channel.send('Welcome to Rock Paper Scissors. Please react to this message with a rock, paper (📰), or scissors emoji to play the game!')

        def check(reaction, user):
            return user == message.author and str(reaction.emoji) in ('📰', '🧱', '✂️')

        try:
            reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
        except asyncio.TimeoutError:
            await channel.send('Timed out. Pleas re-enter command to play again.')

        if str(reaction.emoji) == '📰':
            random_choice = random.randint(1, 3)
            if random_choice == 1:
                await channel.send('You choose paper. I choose rock.')
                await channel.send('You win!')
            if random_choice == 2:
                await channel.send('You choose paper. I choose paper.')
                await channel.send('We tied!')
            if random_choice == 3:
                await channel.send('You choose paper. I choose scissors.')
                await channel.send('I win!')
        elif str(reaction.emoji) == '🧱':
            random_choice = random.randint(1, 3)
            if random_choice == 1:
                await channel.send('You choose rock. I choose rock.')
                await channel.send('We tied!')
            if random_choice == 2:
                await channel.send('You choose rock. I choose paper.')
                await channel.send('I win!')
            if random_choice == 3:
                await channel.send('You choose rock. I choose scissors.')
                await channel.send('You win!')
        elif str(reaction.emoji) == '✂️':
            #do the logic for choosing scissors here

相关问题 更多 >