Error await keeps creating multiple channels instead of checking - python-3.x

Hi I'm having an issue where my code is creating more than one channels. The function of the code should:
Check to see if the channel exists
If Channel does exist don't create a new one.
Otherwise, if the channel does not exist create one.
In a nutshell, how it supposed to work, The on_message event checks for a response from a user in a direct message sent to the bot, their message is then relayed to a guild channel that either already exists otherwise a new one is created before the message is sent.
In this is the issue is that I can't get around it with a check to see if the channel exists, it sure does but it just duplicates everytime a message is sent:
I have tried both these methods to check:
#Check 1
for channel in guild.text_channels:
if channel.name == f"{message.author.name.lower()}{message.author.discriminator}" and channel.name is not None:
await channel.send(embed=embed)
else:
channel_non = await guild.create_text_channel(f'{message.author.name}{message.author.discriminator}', overwrites=overwrites, category=self.bot.get_channel(744944688271720518))
await channel_non.send(embed=embed)
#Check 2
channel_present = False
for channel in guild.text_channels:
if channel.name == f"{message.author.name.lower()}{message.author.discriminator}":
await channel.send(embed=embed)
channel_present = True
if channel_present:
channel_non = await guild.create_text_channel(f'{message.author.name}{message.author.discriminator}', overwrites=overwrites, category=self.bot.get_channel(744944688271720518))
await channel_non.send(embed=embed)
The check 2 code causes the on_message event to do nothing. Help would be much appreciated I don't know where I'm going wrong here.

Honestly, I would just do it by a command.
#client.command()
async def command(ctx):
author = ctx.message.author
guild = ctx.guild
for channel in guild.text_channels:
if channel.name == ctx.message.author:
await channel.send(embed = embed)
return
channel = await guild.create_text_channel(f"{ctx.message.author}", overwrites = overwrites, category = self.bot.get_channel(744944688271720518))
await channel.send(embed = embed)

Related

I want nc!nuke #channel for bot delete all messages (dont channel, only messages on channel)

whos have code on that maybe?
async def nuke(ctx, channel: discord.TextChannel = None):
if channel == None:
await ctx.send("You did not mention a channel!")
return
nuke_channel = discord.utils.get(ctx.guild.channels, name=channel.name)
if nuke_channel is not None:
new_channel = await nuke_channel.clone(reason="Has been Nuked!")
await nuke_channel.delete()
await new_channel.send("THIS CHANNEL HAS BEEN NUKED!")
await ctx.send("Nuked the Channel sucessfully!")
else:
await ctx.send(f"No channel named {channel.name} was found!")```
You will have to use the discord.TextChannel.purge() function.
channel # discord.TextChannel object (most likely the argument)
await channel.purge(limit=100) # Purge a certain amount of messages. You can choose
See the purge() function docs

in discord.py how to to delete others all message

I want to make bot that delete specific server or channel's bot's all message.
but I can find just deleting event message or delete channel's message.
import asyncio
#client.event
async def on_message(message):
content = message.content
guild = message.guild
author = message.author
channel = message.channel
if content == "!delete":
await #delete all bot's message
Deleting all the bot's message in a specific channel can be done with channel.purge
async def on_message(message):
channel = message.channel
if message.content == '!delete':
if len(message.mentions) == 0:
await channel.purge(limit=100, check= lambda x: x.author.id == client.user.id)
else:
mentioned = message.mentions[0]
await channel.purge(limit=100, check= lambda x: x.author.id == mentioned.id)
If you want to delete messages in all channels, just loop it with guild.channels
References:
channel.purge
message.mentions
Note:
The check function should return a bool that tells use whether we should delete the message or not, and takes the message as the parameter.
If you want to delete all messages in specific channel:
guilds_list = [CHANNEL_ID, CHANNEL2_ID, CHANNEL3_ID]
#client.event
async def on_message(message):
if message.channel.id in guilds_list:
await message.delete()
Now bot will delete all messages in specific channel, if you want to delete only bot's messages:
#client.event
async def on_message(message):
if message.channel.id in guilds_list and message.author is client.user:
await message.delete()
Note: this process is automatic. So bot will delete every message it send to specific channel.

Check TextChannel's id and do some actions

Hey everyone I want to delete all messages from a specific text channel I use this method
#client.event
async def on_message(message):
for guild in client.guilds:
for channel in guild.text_channels:
if channel.id == 818522056261369906:
await message.delete()
it works but it delete all text channels messages not just that text channel with the id above
What is the problem
it works but it delete all text channels messages not just that text channel with the id above What is the problem
You're deleting the message upon checking if a certain channel exists in your bot (i.e by checking if that channel exists in any of all the guilds your bot is in). You're not checking if the message's channel's id matches the provided ID or not. That's why it only checks if that specific channel exists and continues to delete the message regardless of weather they're in that specific channel or not.
for channel in guild.text_channels:
# you're checking if a certain channel in guild's
# text_channel matches the ID or not
if channel.id == 818522056261369906:
await message.delete()
But you ought to check if your message's channel's id matches the ID or not. You can do so by message.channel.id == 818522056261369906. So your code should look something like this
if message.channel.id == 818522056261369906:
await message.delete()
The simplest way to do this would be to include get_channel(id) and await purge if you want to continuously delete messages from a channel.
#client.event
async def on_message(message):
channel = client.get_channel(818522056261369906) # this gets the specific channel through id
await channel.purge()
await client.process_commands(message)
However, if you only want to purge it once in a given channel, you would use a #client.command with similar code as above.
#client.command()
async def test(ctx):
channel = ctx.channel # getting the current channel
await channel.purge()
Edit: Don't forget to add your await client.process_commands(message) if you're using both the commands extension and the on_message event!

Discord py mute command specifying reason without time

So I made a mute command and converted it to an optional tempmute yesterday, it worked fine except for when I specified a reason and no time. Obviously that would create a problem but sadly I could not figure out how to fix it. i.e: N?mute user time reason works but N?mute user reason doesn't. If I don't specify the reason nor time it mutes them for 2 years. I want to be able to perm mute people with a reason and also temp mute people with a reason (already works). Hereby the code:
#commands.command()
#commands.has_permissions(manage_roles=True)
async def mute(self, ctx, member: discord.Member, time="670d", *, reason="no reason provided"):
role = discord.utils.get(ctx.guild.roles, name="Muted")
admin = discord.utils.get(ctx.guild.roles, name="Admin")
mod = discord.utils.get(ctx.guild.roles, name="Moderator")
guild = ctx.guild
pfp = member.avatar_url
time_convert = {"s":1, "m":60, "h":3600,"d":86400}
tempmute= int(time[0]) * time_convert[time[-1]]
if admin in member.roles or mod in member.roles:
embed = discord.Embed(title="Mute failed", description="Staff cannot be muted.", color=0xff4654)
await ctx.channel.send(embed=embed)
return
if role in member.roles:
embed = discord.Embed(title="Mute failed", description="**{0}** is already muted!".format(member), color=0xff4654)
await ctx.channel.send(embed=embed)
if role not in guild.roles:
perms = discord.Permissions(send_messages=False, speak=False)
await guild.create_role(name="Muted", permissions=perms)
await member.add_roles(role, reason=reason) #User muted
embed = discord.Embed(title="User muted!", description="**{0}** was succesfully muted by **{1}** for **{2}**.".format(member, ctx.message.author, reason), color=0xff4654)
await ctx.channel.send(embed=embed)
channel = discord.utils.get(guild.channels, name="logs")
embed = discord.Embed(title=" ", description="**{0}** was muted by **{1}**. Reason: **{2}**".format(member, ctx.message.author, reason), color=0xff4654)
embed.set_author(name="{0}".format(member), icon_url=pfp)
await channel.send(embed=embed) #Log
await asyncio.sleep(tempmute)
await member.remove_roles(role)
else:
await member.add_roles(role, reason=reason) #User muted
embed = discord.Embed(title="User muted!", description="**{0}** was succesfully muted by **{1}** for **{2}**.".format(member, ctx.message.author, reason), color=0xff4654)
await ctx.channel.send(embed=embed)
channel = discord.utils.get(guild.text_channels, name="logs")
embed = discord.Embed(title=" ", description="<#{0}> was muted by <#{1}>. Reason: **{2}**".format(member.id, ctx.message.author.id, reason), color=0xff4654)
embed.set_author(name="{0}".format(member), icon_url=pfp)
await channel.send(embed=embed) #Log
await asyncio.sleep(tempmute)
await member.remove_roles(role)
I tried doing time: Optional[int] as someone else suggested but alas it broke it entirely.
To answer bluntly, there isn't a nice way of doing this. What I recommend is that you split the command into two separate ones, mute and tempmute.
If you really do want it as one function, you could do something like this:
import re
async def mute(self, ctx, member: discord.Member, *options):
if len(options) == 2: # User specified date and reason
expiry, reason = options
if len(options) == 1: # User specified one of the above so we need to find out which
if re.match(r"\d+\D", options[0]):
# Regex to check for one or more digit then a single non-digit
expiry = options[0]
reason = None
else:
expiry = None
reason = options[0]
On a slightly unrelated note, you should not do await asyncio.sleep(tempmute). Long sleeps are innacurate and if the bot restarts, the person will never be unmuted. You should create a database with datetimes of when the user should be unmuted and have a looping task which queries the database and unmutes users when their time is up.

Reaction Handling in Discord.py Rewrite Commands

Is there a way to capture a reaction from a command. I have made a command that deletes a channel but I would like to ask the user if they are sure using reactions. I would like to prevent others from reacting to this message (Only the Context Author should React).
So far what I have found is just to use the on_reaction_add() but this can not detect the user who sent the command. I would like to only update the command if the message author is the one who reacted to the message, anyone else, ignore it.
Update: I found that wait_for() does exactly what I want but the issue now is how do I check for if the wrong reaction is set? (i.e if I press the second reaction, delete the message)
if is_admin:
msg = await ctx.send('Clear: Are you sure you would like to purge this entire channel?')
emoji1 = u"\u2705"
emoji2 = u"\u274E"
await msg.add_reaction(emoji=emoji1)
await msg.add_reaction(emoji=emoji2)
def check(reaction, user):
return user == ctx.message.author and reaction.emoji == u"\u2705"
try:
reaction, user = await self.client.wait_for('reaction_add', timeout=10.0, check=check)
except asyncio.TimeoutError:
return await msg.delete()
else:
channel = ctx.message.channel
new_channel = await channel.clone(name=channel.name, reason=f'Clear Channel ran by {ctx.message.author.name}')
await new_channel.edit(position=channel.position)
await channel.delete(reason=f'Clear Channel ran by {ctx.message.author.name}')
await new_channel.send('Clear: Channel has now been cleared.', delete_after=7)
else:
await ctx.send(f"Sorry, you do not have access to this command.", delete_after=5)
Here's a function I use for generating check functions for wait_for:
from collections.abc import Sequence
def make_sequence(seq):
if seq is None:
return ()
if isinstance(seq, Sequence) and not isinstance(seq, str):
return seq
else:
return (seq,)
def reaction_check(message=None, emoji=None, author=None, ignore_bot=True):
message = make_sequence(message)
message = tuple(m.id for m in message)
emoji = make_sequence(emoji)
author = make_sequence(author)
def check(reaction, user):
if ignore_bot and user.bot:
return False
if message and reaction.message.id not in message:
return False
if emoji and reaction.emoji not in emoji:
return False
if author and user not in author:
return False
return True
return check
We can pass the message(s), user(s), and emoji(s) that we want to wait for, and it will automatically ignore everything else.
check = reaction_check(message=msg, author=ctx.author, emoji=(emoji1, emoji2))
try:
reaction, user = await self.client.wait_for('reaction_add', timeout=10.0, check=check)
if reaction.emoji == emoji1:
# emoji1 logic
elif reaction.emoji == emoji2:
# emoji2 logic
except TimeoutError:
# timeout logic

Resources