Python 反应响应中的 Discord Bot

时间:2021-01-28 23:28:11

标签: python discord bots discord.py

我希望当有人对一个表情符号做出反应时,机器人会写成这样的登录聊天:

<块引用>

@Santa 已按 4️⃣

我试图成功,但我被卡住了。

import discord
import time
import random
from discord.ext import commands

client = discord.Client()
bot  = client.user

if message.content.startswith('ººrandgame'):
    original = await message.channel.send('Discover the number I my mind...')

    uno = await original.add_reaction('1️⃣')
    dos = await original.add_reaction('2️⃣')
    tres = await original.add_reaction('3️⃣')
    cuatro = await original.add_reaction('4️⃣')
    cinco = await original.add_reaction('5️⃣')
    seis = await original.add_reaction('6️⃣')
    siete = await original.add_reaction('7️⃣')
    ocho = await original.add_reaction('8️⃣')
    nueve = await original.add_reaction('9️⃣')
    diez = await original.add_reaction('?')
    numbers = ['1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣','?']
    #this part fails::
if(reaction.emoji == "1️⃣"):
            await message.channel.send(author + "has press 1️⃣")

#the same idea with the other numbers 





time.sleep(15)
finalnum = random.choice(numbers)
await message.channel.send('My number is: ' + finalnum)
print(finalnum)

client.run('blabla')

2 个答案:

答案 0 :(得分:1)

您可以使用reaction_wait_for,这将始终等待指定反应的消息输入的作者。

下面我制作了一个简单的用户反应命令,但我会让你决定如何进一步改进它。

message = await ctx.send("React to a number")

one = '1️⃣'
two = '2️⃣'

await message.add_reaction(one)
await message.add_reaction(two)

    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in [one, two]

        member = ctx.author
        while True:
            try:
                reaction, user = await client.wait_for("reaction_add", timeout=600.0, check=check)

                if str(reaction.emoji) == one:
                    await ctx.send("You choose 1")
                                
                if str(reaction.emoji) == two:
                    await ctx.send("You choose 2")

在您的代码中,我还建议使用 asyncio.sleep(15) 而不是 time.sleep,因为这会导致整个机器人停止,这意味着根本没有人可以使用它。 确保import asyncio

你也可以将其设置为命令,而不是使用if message.content.startswith('ººrandgame'): ,你可以使用


@client.command()
async def ººrandgame(ctx):

.... Rest of your code ...

答案 1 :(得分:0)

同上,我觉得少了点什么

import discord
import os
import random
import asyncio
from discord.ext import commands


client = discord.Client()
horaact = time.strftime('%H:%M:%S')
os.system('cls')
os.system('color 1f')

@client.event 
async def on_ready():
    print("Encendido")
    print("logged in as {0.user}".format(client))


@client.command()
async def ººrandgame(ctx):
    message = await ctx.send("React to a number")

    one = '1️⃣'
    two = '2️⃣'

    await message.add_reaction(one)
    await message.add_reaction(two)
    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in [one, two]

        member = ctx.author
        while True:
            try:
                reaction, user = await client.wait_for("reaction_add", timeout=600.0, check=check)
                if str(reaction.emoji) == one:
                    await ctx.send("You choose 1")
                                        
                if str(reaction.emoji) == two:
                    await ctx.send("You choose 2")
client.run('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')

记住,这不是原始代码,只是为了测试你的功能