I am trying to get the number of how many messages have been sent in a channel, and using the logs_from() function wont work because that only accepts a fixed amount of messages to retrieve, how do I do this?
In the discord.py-rewrite branch, there is a TextChannel.history AsyncIterator. If you pass limit=None, it will return all the messages from the channel
#bot.command()
async def message_count(ctx, channel: discord.TextChannel=None):
channel = channel or ctx.channel
count = 0
async for _ in channel.history(limit=None):
count += 1
await ctx.send("There were {} messages in {}".format(count, channel.mention))
You might try passing limit=None to logs_from, but it isn't documented as working that way like it is in the rewrite branch.
Related
I have a bot running in a separate app, but there is a specific variable holding data that I want to also be echo'd on my discord server. The bot itself is huge, but I can pin the specific method here
import discord
import asyncio
import rpChat
global note
class EchoBot(rpChat.ChatClient):
def on_PRI(self, character, message):
super().on_PRI(character, message)
note = character + ": " + message
global note
if message[:1] == "~":
super().PRI("A GameMaster", note)
to try to send this message to discord, I have the following. This is not put in the class above, but just below it, and is not in a class itself:
client = discord.Client()
async def discordEcho():
"""Background task that sends message sent from chat to an appropriate Discord channel
when global messege data is updated"""
await client.wait_until_ready()
channel = client.get_channel(serverroom}
while not client.is_closed():
global note
await channel.send(channel, note)
The data to grab the channel IDs are found in a json
file = open("credentials.json", "r", encoding="utf-8")
info = json.load(file)
file.close()
token = info["discord"]
serverroom = info["serverroom"]
client.loop.create_task(discordEcho())
client.run(token)
When I try this, I obtain:
await client.send_message(channel, message)
AttributeError: 'Client' object has no attribute 'send_message'
And I am unsure why. I have built a bot for both this chat platform, and for discord, but this is the first time I've ever attempted to bridge messages between the two. I am sure what I have offered is clear as mud, but any help would be appreciated, if possible.
Edit:
Edited changes to code. It is working thanks to comment below.
client.send_message is an old, outdated method, it has been replaced with Messageable.send
async def discordEcho():
"""Background task that sends message sent from chat to an appropriate Discord channel
when global messege data is updated"""
await client.wait_until_ready()
channel = client.get_channel(serverroom)
while not client.is_closed():
global note
await channel.send(note)
Side note: you first need to wait_until_ready before getting the channel to wait until the cache is done loading, otherwise channel will be None
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!
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
Hello i want to send a message to multiple channels using channel id's from any server.
I used below code its working fine for a single channel.
#bot.command(pass_context=True)
async def ping(ctx):
channel = bot.get_channel("1234567890")
await bot.send_message(channel, "Pong")
But when i try to add multiple channel id's i getting error TypeError: get_channel() takes 2 positional arguments but 3 were given when i use like below.
channel = bot.get_channel("1234567890", "234567890")
get_channel takes a single argument. You need to loop over all the IDs and send a message to each individually.
#bot.command(pass_context=True)
async def ping(ctx):
channels_to_send = ["1234567890", "234567890"]
for channel_id in channels_to_send:
channel = bot.get_channel(channel_id)
await bot.send_message(channel, "Pong")
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.