【问题标题】:Discord Bot in Python Reaction ResponsePython Reaction Response 中的 Discord Bot
【发布时间】:2021-05-02 21:55:47
【问题描述】:

我希望当有人对一个表情做出反应时,机器人会这样写成聊天日志:

@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')

【问题讨论】:

    标签: python discord bots discord.py


    【解决方案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 ...
    

    【讨论】:

    • @Dharman & Cohen 非常感谢您回答我。但是当我尝试你的代码时,我遇到了一个问题,控制台提取: {note: was defined as: client = discord.Client() } >>> client.run('xxxxxxxxxxxxxxxxxxx') >>> ^ >>>IndentationError : 意外的 unindent 我打开一个新的 .py 进行测试,错误是一样的。我将在另一条评论中粘贴测试代码。从已经非常感谢你;)。
    • @Santa 这不是来自这段代码。您在自己的代码中有不正确的缩进。大概就在if(reaction.emoji == "1️⃣"):附近。
    【解决方案2】:

    同上,我觉得少了点什么

    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')
    

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

    【讨论】:

      猜你喜欢
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 2021-08-30
      • 1970-01-01
      • 2018-07-08
      • 2022-09-28
      • 2020-07-15
      相关资源
      最近更新 更多