Santa
Santa

Reputation: 16

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️⃣

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

Upvotes: 0

Views: 406

Answers (2)

Santa
Santa

Reputation: 16

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

Upvotes: 0

Cohen
Cohen

Reputation: 2415

You can use a reaction_wait_for, this will always wait for the author of the message input of the specified reaction.

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. Make sure to import asyncio

You can also set it as a command, instead of using if message.content.startswith('ººrandgame'): , You can use


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

.... Rest of your code ...

Upvotes: 1

Related Questions