This question already has answers here:
How to make discord.py bot delete its own message after some time?
(3 answers)
Closed 9 months ago.
Trying to get the bot to clear message after waited time but i can't seem to figure out why.
It waits but then doesn't clear the bots message, i think I've got the wrong variable but I'm not sure what it is.
#client.command()
#commands.has_permissions(administrator = True)
async def clear(ctx, amount = 5):
await ctx.channel.purge(limit=amount + 1)
await ctx.send("Cleared " + str(amount) + " messages")
time.sleep(2.5)
await ctx.message.delete()
Your command should be like this. Use asyncio library
await ctx.channel.purge(limit=amount+1)
await asyncio.sleep(your_time)
await ctx.send("Done 👌", hidden=True)
You'd wanna use something like this. It's a basic clear command.
#client.command()
#commands.has_permissions(manage_messages=True)
async def clear(ctx, amout : int):
await ctx.channel.purge(limit=amout)
Related
I am making a discord bot and I tried to make a status command that will make my bot's status start changing, so I thought its working pretty well until I realized that I need it to restart and that I have no idea how to do that, so here is my code without the restart part:
#client.command()
async def status(ctx):
await client.change_presence(activity=discord.Streaming(name='firststatus', url='https://www.twitch.tv/my-channel-name'))
await asyncio.sleep(5)
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='Secondstatus'))
so I did that but I don't know what to put at the end so it'll restart I guess it will be something like client.command.restart.
You can use a simple while loop or the built-in discord.py extension tasks
from discord.ext import tasks
#tasks.loop(seconds=5)
async def change_status():
await client.change_presence(activity=discord.Streaming(name='firststatus', url='https://www.twitch.tv/my-channel-name'))
await asyncio.sleep(5)
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='Secondstatus'))
#client.command()
async def status(ctx):
change_status.start()
The change_status function will loop every 5 seconds and change the presence. You can stop it with change_status.stop()
Reference:
tasks.loop
Loop.start
Loop.stop
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I made this command called p!poll [message] where I want my bot to send an embed with [message] for the description and react with the emojis 👍 and 👎. The command, however, isn't responding and I don't understand why.
#client.command
async def poll(ctx, *, message):
embedVar = discord.Embed(title='Poll', description=f'{message}')
msg = await ctx.channel.send(embed=embedVar)
await msg.add_reaction('👍')
await msg.add_reaction('👎')
Your command is forgetting to call the command, which is the double parenthesis, ()
Simply can be fixed by adding: client.command() to the top where it previously says without the parenthesis
It's better to include "None" in your message decorator, as it allows members to know they must pass a message through, otherwise it would not run the command.
I choose to optionally add some more functionality to your command, (only if you wish for use) and the option of sending it to a different channel, but I have tried this and should work. Hope this helps, change it to whatever you need.
#client.command()
async def poll(ctx, *, message=None):
if message == None:
await ctx.send(f'Cannot create a poll with no message!')
return
questions = [
f"Which channel should your poll be sent to?"
]
answers = []
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
for i in questions:
await ctx.send(i)
try:
msg = await client.wait_for('message', timeout=30.0, check=check)
except asyncio.TimeoutError:
await ctx.send("Setup timed out, please be quicker next time!")
return
else:
answers.append(msg.content)
try:
c_id = int(answers[0][2:-1])
except:
await ctx.send(
f"You didn't mention a channel properly, please format like {ctx.channel.mention} next time."
)
return
channel = client.get_channel(c_id)
embed = discord.Embed(title="Poll", description='{message}', colour=discord.Color.black())
message = await channel.send(embed=embed )
await message.add_reaction('👍')
await message.add_reaction('👎')
I've been working on this piece of code:
#client.event
async def lol(message,ctx):
if ctx.author.id == <user_id>:
await message.add_reaction('❤️')
else:
return
I am pretty sure that it is developed correctly, yet I am not getting the desired results.
I am pretty sure that if you check the console, an HTTP error would have been encountered. That's because you directly pasted the emoji in the function parameter. You have to add its unicode value. In your case:
await message.add_reaction(u'\u2764')
You are using an event decorator rather than a command, unless you were trying to detect if the user reacted, but its still completely off. What it looks like, the command that is called will send a message and the bot will react to it.
#client.command()
async def lol(ctx):
message = await ctx.send(f"{ctx.author.mention}, lol")
await message.add_reaction('😂')
If you then want to detect if the user had reacted to the emoji, you can use a event, but an on_reaction_add:
#client.event
async def on_reaction_add(reaction, user):
if reaction.emoji == '😂':
await user.send("Lol")
I have a mute command in my bot but i want to add a timer on it, not really sure how to tho, this is my current code. It adds the code correctly and it all sends correctly but the duration i'm not really sure about. Any help would be appreciated!
Edit: I got the duration working, but how would i convert it into minutes/hours etc.?
#client.command()
#commands.has_permissions(manage_messages=True)
async def mute(ctx, member: discord.Member, mute_time : int, *, reason=None):
role = discord.utils.get(ctx.guild.roles, name="[Muted]")
await member.add_roles(role)
await ctx.send(f'**Muted** {member.mention}\n**Reason: **{reason}\n**Duration:** {mute_time}')
embed = discord.Embed(color=discord.Color.green())
embed.add_field(name=f"You've been **Muted** in {ctx.guild.name}.", value=f"**Action By: **{ctx.author.mention}\n**Reason: **{reason}\n**Duration:** {mute_time}")
await member.send(embed=embed)
await asyncio.sleep(mute_time)
await member.remove_roles(role)
await ctx.send(f"**Unmuted {member.mention}**")
I use your code in minimal example and it's work well. What problems do you have? Example:
#client.command()
#commands.has_permissions(manage_messages=True)
async def mute(ctx, mute_time : int):
await ctx.send("Muted")
await asyncio.sleep(mute_time)
await ctx.send("Unmuted")
Exactly one minute passed. Result:
This question already has answers here:
Why doesn't multiple on_message events work?
(2 answers)
Closed 3 years ago.
I'm fairly new to this format of discord.py and I can't seem to get two commands to work.
For example, I have the command; 'hello' and the command 'valid'. Whenever I activate the bot, it only responds to either 'valid' or 'hello', never both.
Is there some way to fix this?
I've tried very little as this problem is very new to me and I have no idea how to tackle it.
Here's the code that I've used for commands:
#client.event
async def on_message(message):
if message.content.startswith('!hello'):
messages= ["*tips hat* G'day ", "Yeehaw pardner, ", "Howdy, ", "Gutentag! "]
await client.send_message(message.channel, random.choice(messages) + message.author.mention)
#client.event
async def on_message(message):
if message.content.startswith('!valid'):
rannum = random.randint(0, 100)
await client.send_message(message.channel, (message.author.mention + " is",rannum,"% valid!"))
client.run(TOKEN)
No error messages show up when this happens. I will appreciate any help possible in this situation!
Like what #Patrick Haugh mentioned, you can only have 1 of on_message event.
Plus, you probably only need one of that event. Since on_message defines a event that occurs when you recieve a message from someone. You can just check what the message's content is and do actions based on that.
Like so:
#client.event
async def on_message(message):
if message.content.startswith('!valid'):
rannum = random.randint(0, 100)
await client.send_message(message.channel, (message.author.mention + " is",rannum,"% valid!"))
elif message.content.startswith('!hello'):
messages= ["*tips hat* G'day ", "Yeehaw pardner, ", "Howdy, ", "Gutentag! "]
await client.send_message(message.channel, random.choice(messages) + message.author.mention)