I'm attempting to make an offer command in which the bot detects whether the mentioned user reacts with check or x.
#bot.command()
#commands.has_any_role("Franchise Owner", "General Manager", "Head Coach")
async def offer(ctx, member:discord.Member, reaction):
embed = discord.Embed()
embed.add_field(name="<a:Loading:768095883664424971> Incoming Offer", value=f"The <:DallasCowboys:788796627161710592> have offered {member.mention}.")
offer_sent = await ctx.send(embed=embed)
await offer_sent.add_reaction("<a:CheckMark:768095274949935146>")
await offer_sent.add_reaction("<a:XMark:768095331555606528>")
await member.send("You have been offered to the <:DallasCowboys:788796627161710592>. You have 30 minutes to accept/decline.")
await asyncio.sleep(1800) # Replace "30" with 1800, because 1800 in seconds is 30 min.
await offer_sent.delete()
def on_reaction(reaction, member:discord.Member):
channel = member.channel
def check(reaction, user):
return user == member and str(reaction.emoji) == '<a:CheckMark:768095274949935146>'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send(f"{member.mention} hasn't reacted in time.")
else:
await channel.send(f"{member.mention} has accepted <:DallasCowboys:788796627161710592> offer.")
I removed the reaction from the offer function as I have no clue what it did as it wasn't used in the code. Then I removed the on_reaction function but moved the code from inside it before the asyncio.sleep(1800).
#bot.command()
#commands.has_any_role("Franchise Owner", "General Manager", "Head Coach")
async def offer(ctx, member:discord.Member):
embed = discord.Embed()
embed.add_field(name="<a:Loading:768095883664424971> Incoming Offer", value=f"The <:DallasCowboys:788796627161710592> have offered {member.mention}.")
offer_sent = await ctx.send(embed=embed)
await offer_sent.add_reaction("<a:CheckMark:768095274949935146>")
await offer_sent.add_reaction("<a:XMark:768095331555606528>")
await member.send("You have been offered to the <:DallasCowboys:788796627161710592>. You have 30 minutes to accept/decline.")
channel = ctx.channel
def check(reaction, user):
return user == member and str(reaction.emoji) == '<a:CheckMark:768095274949935146>'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send(f"{member.mention} hasn't reacted in time.")
else:
await channel.send(f"{member.mention} has accepted <:DallasCowboys:788796627161710592> offer.")
await asyncio.sleep(1800) # Replace "30" with 1800, because 1800 in seconds is 30 min.
await offer_sent.delete()
Related
I am having troubles gettimg my commands to work, it keeps telling me that they do not exist when they clearly do. I've been stumped on this for hours and can't figure out why its not working. I've tried doing commands.command() instead of #commands.command(), but no luck, i tried explicitly defining the name of the command #commands.command(name="play") but still no luck
import youtube_dl
import pafy
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
#bot.event
async def on_ready():
print(f"{bot.user.name} is ready.")
bot.run("....")
class Player(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.song_queue = {}
self.setup()
def setup(self):
for guild in self.bot.guilds:
self.song_queue[guild.id] = []
async def check_queue(self, ctx):
if len(self.song_queue[ctx.guild.id]) > 0:
await self.play_song(ctx, self.song_queue[ctx.guild.id][0])
self.song_queue[ctx.guild.id].pop(0)
async def search_song(self, amount, song, get_url=False):
info = await self.bot.loop.run_in_executor(None, lambda: youtube_dl.YoutubeDL({"format" : "bestaudio", "quiet" : True}).extract_info(f"ytsearch{amount}:{song}", download=False, ie_key="YoutubeSearch"))
if len(info["entries"]) == 0: return None
return [entry["webpage_url"] for entry in info["entries"]] if get_url else info
async def play_song(self, ctx, song):
url = pafy.new(song).getbestaudio().url
ctx.voice_client.play(discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(url)), after=lambda error: self.bot.loop.create_task(self.check_queue(ctx)))
ctx.voice_client.source.volume = 0.5
#commands.command()
async def join(self, ctx):
if ctx.author.voice is None:
return await ctx.send("You are not connected to a voice channel, please connect to the channel you want the bot to join.")
if ctx.voice_client is not None:
await ctx.voice_client.disconnect()
await ctx.author.voice.channel.connect()
#commands.command()
async def leave(self, ctx):
if ctx.voice_client is not None:
return await ctx.voice_client.disconnect()
await ctx.send("I am not connected to a voice channel.")
#commands.command(name="play")
async def play(self, ctx, *, song=None):
if song is None:
return await ctx.send("You must include a song to play.")
if ctx.voice_client is None:
return await ctx.send("I must be in a voice channel to play a song.")
# handle song where song isn't url
if not ("youtube.com/watch?" in song or "https://youtu.be/" in song):
await ctx.send("Searching for song, this may take a few seconds.")
result = await self.search_song(1, song, get_url=True)
if result is None:
return await ctx.send("Sorry, I could not find the given song, try using my search command.")
song = result[0]
if ctx.voice_client.source is not None:
queue_len = len(self.song_queue[ctx.guild.id])
if queue_len < 10:
self.song_queue[ctx.guild.id].append(song)
return await ctx.send(f"I am currently playing a song, this song has been added to the queue at position: {queue_len+1}.")
else:
return await ctx.send("Sorry, I can only queue up to 10 songs, please wait for the current song to finish.")
await self.play_song(ctx, song)
await ctx.send(f"Now playing: {song}")
#commands.command()
async def search(self, ctx, *, song=None):
if song is None: return await ctx.send("You forgot to include a song to search for.")
await ctx.send("Searching for song, this may take a few seconds.")
info = await self.search_song(5, song)
embed = discord.Embed(title=f"Results for '{song}':", description="*You can use these URL's to play an exact song if the one you want isn't the first result.*\n", colour=discord.Colour.red())
amount = 0
for entry in info["entries"]:
embed.description += f"[{entry['title']}]({entry['webpage_url']})\n"
amount += 1
embed.set_footer(text=f"Displaying the first {amount} results.")
await ctx.send(embed=embed)
#commands.command()
async def queue(self, ctx): # display the current guilds queue
if len(self.song_queue[ctx.guild.id]) == 0:
return await ctx.send("There are currently no songs in the queue.")
embed = discord.Embed(title="Song Queue", description="", colour=discord.Colour.dark_gold())
i = 1
for url in self.song_queue[ctx.guild.id]:
embed.description += f"{i}) {url}\n"
i += 1
embed.set_footer(text="Thanks for using me!")
await ctx.send(embed=embed)
#commands.command()
async def skip(self, ctx):
if ctx.voice_client is None:
return await ctx.send("I am not playing any song.")
if ctx.author.voice is None:
return await ctx.send("You are not connected to any voice channel.")
if ctx.author.voice.channel.id != ctx.voice_client.channel.id:
return await ctx.send("I am not currently playing any songs for you.")
poll = discord.Embed(title=f"Vote to Skip Song by - {ctx.author.name}#{ctx.author.discriminator}", description="**80% of the voice channel must vote to skip for it to pass.**", colour=discord.Colour.blue())
poll.add_field(name="Skip", value=":white_check_mark:")
poll.add_field(name="Stay", value=":no_entry_sign:")
poll.set_footer(text="Voting ends in 15 seconds.")
poll_msg = await ctx.send(embed=poll) # only returns temporary message, we need to get the cached message to get the reactions
poll_id = poll_msg.id
await poll_msg.add_reaction(u"\u2705") # yes
await poll_msg.add_reaction(u"\U0001F6AB") # no
await asyncio.sleep(15) # 15 seconds to vote
poll_msg = await ctx.channel.fetch_message(poll_id)
votes = {u"\u2705": 0, u"\U0001F6AB": 0}
reacted = []
for reaction in poll_msg.reactions:
if reaction.emoji in [u"\u2705", u"\U0001F6AB"]:
async for user in reaction.users():
if user.voice.channel.id == ctx.voice_client.channel.id and user.id not in reacted and not user.bot:
votes[reaction.emoji] += 1
reacted.append(user.id)
skip = False
if votes[u"\u2705"] > 0:
if votes[u"\U0001F6AB"] == 0 or votes[u"\u2705"] / (votes[u"\u2705"] + votes[u"\U0001F6AB"]) > 0.79: # 80% or higher
skip = True
embed = discord.Embed(title="Skip Successful", description="***Voting to skip the current song was succesful, skipping now.***", colour=discord.Colour.green())
if not skip:
embed = discord.Embed(title="Skip Failed", description="*Voting to skip the current song has failed.*\n\n**Voting failed, the vote requires at least 80% of the members to skip.**", colour=discord.Colour.red())
embed.set_footer(text="Voting has ended.")
await poll_msg.clear_reactions()
await poll_msg.edit(embed=embed)
if skip:
ctx.voice_client.stop()
#commands.command()
async def pause(self, ctx):
if ctx.voice_client.is_paused():
return await ctx.send("I am already paused.")
ctx.voice_client.pause()
await ctx.send("The current song has been paused.")
#commands.command()
async def resume(self, ctx):
if ctx.voice_client is None:
return await ctx.send("I am not connected to a voice channel.")
if not ctx.voice_client.is_paused():
return await ctx.send("I am already playing a song.")
ctx.voice_client.resume()
await ctx.send("The current song has been resumed.")
async def setup():
await bot.wait_until_ready()
bot.add_cog(Player(bot))
bot.loop.create_task(setup())
You can try replacing :
async def setup():
await bot.wait_until_ready()
bot.add_cog(Player(bot))
bot.loop.create_task(setup())
With just :
bot.add_cog(Player(bot))
Moreover, put you run statement after all setup operations, so you have :
# Cog declaration
bot.add_cog(Player(bot))
bot.run('....')
run() should be executed as last function because it starts bot and it runs until you stop bot.
And all code which you have after run() is executed after you stop bot.
bot.loop.create_task(setup())
bot.run(TOKEN)
#commands.command()
Write this down where you wrote it down.
#bot.command()
Because:
bot = commands.Bot(command_prefix="!", intents=intents)
Since you have assigned a "bot" variable here, the name of the variable is added first, and then the "command" is added.
I am trying to create a command wherein if you react on the embed, the bot sends something back. If I add this code to a cog, it won't work. If possible, can you tell me why?
#bot.command(aliases=['test','t'])
async def Test(self, ctx):
TestEmbed = discord.Embed(title="Definition", description="This is just a test embed", color=11027200)
TestEmbed.add_field(name="\u200b", value="▹❁❁▹❁◃❁❁◃",inline=False)
emojis = ['⬅️','➡️']
TestEmbedAdd = await ctx.send(embed=TestEmbed)
for emoji in emojis:
await TestEmbedAdd.add_reaction(emoji)
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in ['⬅️', '➡️']
try:
reaction, user = await bot.wait_for('reaction_add', timeout=5, check=check)
if reaction.emoji == '➡️':
await ctx.send("Reaction 2!")
elif reaction.emoji == '⬅️':
await ctx.send("Reaction 1!")
except asyncio.TimeoutError:
await ctx.send("Time is out!")
With the help of the people in the comment section, I have found the problem with my code. I had to change bot.command into commands.command. (I've tried both bot and command, and it still works splendidly). The other crucial thing I had to add was "self" under bot.wait_for. Without the self, the command wouldn't work. Thank you so much for the help.
#commands.command(aliases=['test','t'])
async def Testing(self, ctx):
TestEmbed = discord.Embed(title="Definition", description="This is just a test embed", color=11027200)
TestEmbed.add_field(name="\u200b", value="▹❁❁▹❁◃❁❁◃",inline=False)
emojis = ['⬅️','➡️']
TestEmbedAdd = await ctx.send(embed=TestEmbed)
for emoji in emojis:
await TestEmbedAdd.add_reaction(emoji)
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in ['⬅️', '➡️']
try:
reaction, user = await self.bot.wait_for('reaction_add', timeout=5, check=check)
if reaction.emoji == '➡️':
await ctx.send("Reaction 2!")
elif reaction.emoji == '⬅️':
await ctx.send("Reaction 1!")
except asyncio.TimeoutError:
await ctx.send("Time is out!")
you need #commands.command instead of #bot.command
#client.command()
async def ra(ctx):
embed = discord.Embed(
colour=discord.Colour.blue(),
title="RIDEALONG REQUEST",
description=str(ctx.author.mention) + " IS REQUESTING A RIDEALONG IN SERVER."
)
embed.add_field(name="For FTOs", value="Please react to this message to accept the Ride-Along.", inline=False)
embed.timestamp = datetime.utcnow()
embed.set_footer(text= "This message will automatically delete in two hours")
msg = await ctx.send(embed=embed, delete_after=7200)
await msg.add_reaction('✔️')
await msg.add_reaction('❌')
await ctx.message.delete()
enter image description here
You can use different events. Mostly we wait_for a reaction_add.
I personally use the following:
try:
reaction, user = await client.wait_for('reaction_add')
while user == client.user:
reaction, user = await client.wait_for('reaction_add')
if str(reaction.emoji) == "YourEmoji":
# Do what you want to do
except Exception: # Timeout can be added for example
return
Here we check that the bot reaction is not counting as a valid reaction but instead a user one.
The full code would be:
#client.command()
async def ra(ctx):
embed = discord.Embed(
colour=discord.Colour.blue(),
title="RIDEALONG REQUEST",
description=str(ctx.author.mention) + " IS REQUESTING A RIDEALONG IN SERVER.")
embed.add_field(name="For FTOs", value="Please react to this message to accept the Ride-Along.", inline=False)
embed.timestamp = datetime.datetime.utcnow()
embed.set_footer(text="This message will automatically delete in two hours")
msg = await ctx.send(embed=embed, delete_after=7200)
await msg.add_reaction('✔️')
await msg.add_reaction('❌')
await ctx.message.delete()
try:
reaction, user = await client.wait_for('reaction_add')
while user == client.user:
reaction, user = await client.wait_for('reaction_add')
if str(reaction.emoji) == "✔️":
# Do what you want to do
except Exception:
return
This is my help command:
#client.command(pass_context=True, aliases=['cmd', 'commands'])
async def help(ctx):
embed = discord.Embed(colour=discord.Colour.blurple())
embed=discord.Embed(title="", description="This is a list of everything Otay! can do! If you need additional help with Otay! join the [**support server**](https://discord.gg/Wa9DpJGEKv)", color=0x7289da)
embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/776181059757932615/784819540496351233/otay2.png")
embed.add_field(name='`🎉` ***Fun***', value='`8ball`, `coinflip`, `dice`, `joke`, `say`, `fact`, `ss`', inline=False)
embed.add_field(name='`⚒️` ***Moderation***', value='`kick`, `ban`, `unban`, `nick`, `purge`, `slowmode`, `mute`, `unmute`, `lock`, `unlock`', inline=False)
embed.add_field(name='`⚠️` ***Setup***', value='`prefix`, `welcome`', inline=False)
embed.add_field(name='`🕹️` ***Games***', value='`rps`, `tictactoe`', inline=False)
embed.add_field(name='`🎁` ***Giveaway***', value='`gstart`', inline=False)
embed.add_field(name='`📷` ***Images***', value='`cat`, `dog`, `fox`, `koala`, `panda`, `meme`, `blursed`', inline=False)
embed.add_field(name='`🔧` ***Utilities***', value='`avatar`, `ping`, `poll`, `serverinfo`, `botinfo`, `userinfo`, `bitcoin`, `snipe`, `createinvite`, `password`', inline=False)
embed.add_field(name="`🎫` ***Ticket***", value="`configureticket`", inline=False)
embed.set_footer(icon_url=f"https://cdn.discordapp.com/attachments/776181059757932615/784819540496351233/otay2.png", text=ctx.author)
embed.timestamp = datetime.datetime.now()
msg = await ctx.send(embed=embed)
def checkifnotbot(reaction, user):
return user != client.user
await msg.add_reaction('🎉')
await msg.add_reaction('⚒️')
await msg.add_reaction('⚠️')
await msg.add_reaction('🕹️')
await msg.add_reaction('🎁')
await msg.add_reaction('📷')
await msg.add_reaction('🔧')
await msg.add_reaction('🎫')
await msg.add_reaction('🔗')
reaction, user = await client.wait_for("reaction_add", check=checkifnotbot)
if str(reaction.emoji) == "🎉":
embedfun=discord.Embed(title="`🎉`Help Fun", color=0x7298da)
await msg.edit(embed=embedfun)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "⚒️":
embedmod=discord.Embed(title="`⚒️`Help Moderation", color=0x7298da)
await msg.edit(embed=embedmod)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "⚠️":
embedsetup=discord.Embed(title="`⚠️`Setup", color=0x7289da)
embedsetup.timestamp = datetime.datetime.now()
await msg.edit(embed=embedsetup)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "🕹️":
embedgames=discord.Embed(title="`🕹️Help Games`", color=0x7289da)
await msg.edit(embed=embedgames)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "🎁":
embedgiveaway=discord.Embed(title="`🎁`Help Giveaway", color=0x7298da)
await msg.edit(embed=embedgiveaway)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "📷":
embedimages=discord.Embed(title="`📷`Help Images", color=0x7298da)
embedimages.timestamp = datetime.datetime.now()
await msg.edit(embed=embedimages)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "🔗":
embedlinks=discord.Embed(title="`🔗`Links", color=0x7289da)
await msg.edit(embed=embedlinks)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "🔧":
embedutils=discord.Embed(title="`🔧`Help Utilities", color=0x7298da)
embedutils.timestamp = datetime.datetime.now()
await msg.edit(embed=embedutils)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "🎫":
embedticket=discord.Embed(title="`🎫`Help Ticket", color=0x7289da)
await msg.edit(embed=embedticket)
await msg.clear_reactions()
await msg.add_reaction('↩️')
def checkifbot(reaction, user):
return user != client.user
reaction, user = await client.wait_for("reaction_add", check=checkifbot)
if str(reaction.emoji) == "↩️":
await msg.edit(embed=embed)
await msg.clear_reactions()
What i want to happen The embed "resets" it adds all the reactions again and just worked like if you typed -help.
My problem I don't know how to make it edit to the normal help command, because if it edits back and you need to add the reactions again you need to make like a million wait_fors
What i've tried
def checkifbot(reaction, user):
return user != client.user
reaction, user = await client.wait_for("reaction_add", check=checkifbot)
if str(reaction.emoji) == "↩️":
await msg.edit(embed=embed)
await msg.clear_reactions()
Problem with this is that i need to make so many wait_fors because if people want to see another category they have to click the reaction again.
And i tried doing this but this just sends it again and doesnt edit it
def checkifbot(reaction, user):
return user != client.user
reaction, user = await client.wait_for("reaction_add", check=checkifbot)
if str(reaction.emoji) == "↩️":
await help(ctx)
So is there a way to do something like: await msg.edit(help)(ctx)? Or how can i solve this problem?
You can solve this by creating a simple get_help function, that is
async def get_help(ctx):
return embed
# whatever u are currently doing in help(ctx), but return embed instead of sending
async def add_reactions(msg, ctx):
#add reactions
#add wait_for
async def help(ctx):
msg = await ctx.send(embed=get_help(ctx))
await add_reactions(msg, ctx)
def checkifbot(reaction, user):
return msg.id == reaction.message.id and user != client.user #improved check
reaction, user = await client.wait_for("reaction_add", check=checkifbot)
if str(reaction.emoji) == "↩️":
await msg.edit(embed = get_help(ctx))
However, you need not go through this much hassle and use discord.py's HelpCommand. which might be easier to implement and modify as per need.
I am making a discord bot with rewrite, when the command runs, the event must finish, but if I want to execute another command I can't beacase the previous one it's not finished and It will send the other messages, how can I stop that?
#client.event
async def on_message(message):
def check(m):
return m.channel == message.channel and m.author != client.user
if message.content.startswith("!order"):
channel = message.author
await channel.send("in game name")
in_game_name = await client.wait_for('message', check=check)
await channel.send("in game ID")
in_game_ID = await client.wait_for('message', check=check)
await channel.send("cargo type")
cargo_type = await client.wait_for('message', check=check)
await channel.send("cargo limit")
cargo_limit = await client.wait_for('message', check=check)
await channel.send("storage")
storage = await client.wait_for('message', check=check)
await channel.send("priority")
priority = await client.wait_for('message', check=check)
You could raise an exception in your check if it sees a certain word. Here, if the bot sees the message CANCEL it will cancel the command:
#client.event
async def on_message(message):
def check(m):
if m.channel == message.channel and m.content == "CANCEL":
raise ValueError("Cancelled command")
return m.channel == message.channel and m.author != client.user
if message.content.startswith("!order"):
channel = message.author
await channel.send("in game name")
in_game_name = await client.wait_for('message', check=check)