简体   繁体   English

Discord Bot in Python Reaction Response

[英]Discord Bot in Python Reaction Response

I want that when someone reacts to one emoji the bot writes as a log on chat like this:我希望当有人对一个表情符号做出反应时,机器人会像这样记录聊天记录:

@Santa has press 4️⃣ @Santa 已按4️⃣

I tried to make it but I'm stuck.我试图做到,但我被困住了。

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

You can use a reaction_wait_for , this will always wait for the author of the message input of the specified reaction.您可以使用reaction_wait_for ,这将始终等待指定反应的消息输入的作者。

Below I've made a simple user reaction command but I'll leave it up to you how you would further like to improve it.下面我做了一个简单的用户反应命令,但我会把它留给你,你想如何进一步改进它。

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

In your code, I would also reccomend using asyncio.sleep(15) instead of time.sleep , as this causes the whole bot to stop, meaning no one can use it at all.在您的代码中,我还建议使用asyncio.sleep(15)而不是time.sleep ,因为这会导致整个机器人停止,这意味着根本没有人可以使用它。 Make sure to import asyncio确保import asyncio

You can also set it as a command, instead of using if message.content.startswith('ººrandgame'): , You can use你也可以将其设置为命令,而不是使用if message.content.startswith('ººrandgame'): ,你可以使用


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

.... Rest of your code ...

Ibidem, I think that something is missing同上,我觉得少了点什么

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

Remember, this is not the original code, it's just a test for trying your funtion请记住,这不是原始代码,它只是用于尝试您的功能的测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM