I have this code that sends a message with 4 reactions, is there a way to take the first reaction a user inputs (it has to take only one input if the user chooses another option and it overrides the first one that is also fine) and save it as a variable to use later on?
import discord
import os
client = discord.Client()
some_list = []
msg = None
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
msg = await message.channel.send("**Question 1?**")
reactions = ['1️⃣','2️⃣','3️⃣','4️⃣']
for emoji in reactions:
await msg.add_reaction(emoji)
#client.event
async def on_reaction_add(reaction, user):
if reaction.message == msg:
some_list.append(user)
client.run("token")
Thanks in Advance!
You should really use wait_for an example of how to use this can be found on my previous answer: Getting input from reactions not working discord.py
You should really use a dictionary if you don't want to go with wait_for.
import discord
import os
client = discord.Client()
some_dict = {}
msg = None
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
msg = await message.channel.send("**Question 1?**")
reactions = ['1️⃣','2️⃣','3️⃣','4️⃣']
for emoji in reactions:
await msg.add_reaction(emoji)
#client.event
async def on_reaction_add(reaction, user):
if reaction.message == msg:
some_dict[user.id] = str(reaction.emoji)
client.run("token")
You can then access it using some_dict[user.id]
Related
I am getting a syntaxError stating that await is outside of the function. Can anyone help me with a fix/explanation?
import discord
import os
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):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
my_secret = os.environ('TOKEN')
client.run(os.environ('TOKEN'))
This is my code but I cant get the loop to work.
import discord
from discord.ext import tasks, commands
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):
if message.author == client.user:
return
if message.content.startswith('!test'):
await message.channel.send('Working')
#tasks.loop(seconds = 10)
async def myLoop():
channel = client.get_channel(899698630271856640)
await channel.send('1')
To start a discord.py loop, you must run it under the on_ready function. Add myLoop.run() in the function.
Im new to python so I cant see where I have made an error.
import discord
import os
from config import TOKEN
client = discord.Client()
#Sends a message in terminal if it has worked
#client.event
async def on_ready():
print('We have logged in as {0.user}'
.format(client))
#Sends a message if !test is included in a sent message
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!test'):
await message.channel.send('test')
#client.event
async def on_member_join(self, member):
guild = member.guild
if guild.system_channel is not None:
to_send = 'Welcome {0.mention} to {1.name}'.format(member, guild)
await guild.system_channel.send(to_send)
client.run(TOKEN)
I have this issue where I want to take input as a command so the user types !ping and my prefix is !, but for some reason it does not direct towards:
#client.command()
async def ping(ctx):
print("Someone asked for their latency")
for some reason it directs it to:
#client.event
async def on_message(message):
print("Someone send a message")
I want when a user types !ping it directs it to ping(ctx) and not on_message(message) like it is doing right not. This is my full code:
import discord
from discord.ext import commands
intents = discord.Intents.all()
client = commands.Bot(command_prefix="!", intents=intents)
#client.event
async def on_ready():
print("I am ready")
#client.event
async def on_member_join(member):
join_embed = discord.Embed(title=f'{member.name} Joined :)',
description=f"Hey {member.name} remember that addison is weird", colour=16366985)
join_embed.set_image(url="https://www.desicomments.com/wp-content/uploads/2017/07/Hi.gif")
await member.guild.system_channel.send(content=None, embed=join_embed)
print("joined")
#client.event
async def on_member_remove(member):
left_embed = discord.Embed(title=f'{member.name} Left :(',
description=f"Bye Bye {member.name}", colour=16726802)
left_embed.set_image(url="http://www.reactiongifs.com/wp-content/uploads/2012/12/byebye.gif")
await member.guild.system_channel.send(content=None, embed=left_embed)
print("left")
#client.event
async def on_message(message):
print("Someone sent a message")
#client.command()
async def ping(ctx):
print("Someone asked for their latency")
client.run('hajslkdhasjlkdhjwheq')
You can simply add await client.process_commands(message) as the last line in on_message() to fix this problem
from discord.ext import commands
load_dotenv("token.env")
token = os.getenv("Token")
bot = commans.Bot(command_prefix="!")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("!"):
await bot.process_commands(message)
return
else:
author = message.author
await message.channel.send(message.content)
How can i change the code in line 15 to send the message to "author"?
You can use Member.create_dm() for this. You can modify your code as follows:
from discord.ext import commands
load_dotenv("token.env")
token = os.getenv("Token")
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("!"):
await bot.process_commands(message)
return
else:
c = await message.author.create_dm()
await c.send(message.content)
You also misspelt commands in bot = commands.Bot(... :)
You could use await ctx.author.send() so that whoever runs the command would get it sent in there dms
from discord.ext import commands
load_dotenv("token.env")
token = os.getenv("Token")
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("!"):
await bot.process_commands(message)
return
else:
author = message.author
await ctx.author.send(message.content)