Check TextChannel's id and do some actions - python-3.x

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!

Related

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.

TwitchIO: Delete a single message in channel

How can I delete a single message sent by a user using TwitchIO?
#bot.event
async def event_message(ctx):
await ctx.content.delete() # Does not work
await ctx.content.remove() # Does not work
await ctx.channel.timeout(ctx.author, 1) # Does not work
The question is older, but I'll answer it anyway.
Twitchio doesn't directly support this.
But you can delete individual messages in Twitch Chat, see the Twitch IRC documentation.
CLEARMSG (Twitch Commands)
You need the message ID for this. You get the ID in the message tags.
Message tags
Code example:
async def event_message(message):
if not message.author.name == self.bot.nick:
message_id = message.tags['id']
await message.channel.send(f"/delete {message_id}")
If you want to timeout someone, do the following:
await message.channel.timeout(message.author.name, 120, f"reason")
Twitchio documentation Channel.timeout

Error await keeps creating multiple channels instead of checking

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)

Moving a user to the message author's voice channel

Trying to make my discord bot move a user to the voice channel that the message author is in. E.g. I write !move #john Then the bot would move "john" to my voice channel.
# command to move a user to current channel
#bot.command()
async def move(ctx,member:discord.Member=None):
channel= discord.utils.get(ctx.guild.voice_channels)
if not member:
await ctx.send("Who am I trying to move? Use !move #user")
await member.move_to(channel)
At the moment it moves the users, but only to the first voice channel in the server. How do I get it to move it to the author's voice channel?
The VoiceChannel the author is in is stored in ctx.author.voice.channel. Notably, .voice and .channel can be None, so we need check for that
#bot.command()
async def move(ctx,member:discord.Member=None):
if ctx.author.voice and ctx.author.voice.channel:
channel = ctx.author.voice.channel
else:
await ctx.send("You are not connected to voice!")
if not member:
await ctx.send("Who am I trying to move? Use !move #user")
await member.move_to(channel)

Delete all the bot's messages in a channel

I am trying to create a command in my bot that will delete all the bot's messages only. Is there a way to iterate through all the channel's messages and if it is a message the bot sent, the bot would delete it? I've understood delete_message but I can't figure out how to iterate through all the channel's messages, if that is even possible.
The following code wouldn't iterate through all the channel's messages, but it would delete the message if the author ID is 383804325077581834:
#bot.event
async def on_message(message):
if message.author.id == '383804325077581834':
await bot.delete_message(message)
383804325077581834 is my bot's ID. So I would like to know how I can iterate through all channel messages and delete those that were sent by my bot. Thank you so much!
EDIT: Tried doing this:
#bot.command(pass_context=True)
async def delete(ctx, number):
msgs = []
number = int(number)
async for msg in bot.logs_from(ctx.message.channel, limit=number):
if msg.author.id == '383804325077581834':
msgs.append(msg)
await bot.delete_messages(msgs)
But I get the error discord.ext.commands.errors.MissingRequiredArgument: number is a required argument that is missing.
#bot.command(pass_context=True)
async def delete(ctx):
msgs = []
async for msg in bot.logs_from(ctx.message.channel):
if msg.author.id == '383804325077581834':
msgs.append(msg)
await bot.delete_messages(msgs)
The command you tried using needed a number parameter which would delete that number of messages from the channel.
The logs_from function will delete all messages from that channel if the limit is not passed into it.
EDIT: Forgot to remove number, whoops.

Resources