I'm trying to store messages with a discord bot, so that I can learn how the elements of messages vary between messages.
However i am new to some aspects of this coding- i.e. decorators. Currently the piece of my bots code that interacts with messages is this:
messages=[]
#bot.event
async def on_message(message,messages):
print("someone said something")
messages=messages+message
if message.author == bot.user:
return messages
I think this is wrong. What I am trying to do is add a message to messages each time the event happens, so that I can later go through that variable and see how the different elements of messages change.
How do i change the above to allow that?
You can use only 1 parameter in on_message event. Also, you can't append things to a list with +. And Also storing data in variable is not a good idea because whenever you restart the bot, it'll be deleted. You can simply store them in a txt file.
#bot.event
async def on_message(message):
print("someone said something")
file = open('messages.txt', 'a')
file.write(message.content + '\n')
file.close()
EDIT
If you want to store all the information of the message, you can do:
file.write(f'{message}\n')
or
file.write(str(message) + '\n')
Related
I mean when it went long after the interaction has been created and the user again chooses a item from selectmenu and I want a bot to respond to the user
Code callbakck:
async def callback(iter:discord.Interaction):
if menu.values[0] == '1':
if iter.is_expired is True:
await iter.response.send_message(embed=discord.Embed(
title='Error!',
description='This interaction has expired. Please enter the appropriate command again so that it will work if necessary.',
color=0xff0000
))
else:
await iter.response.send_message(embed=discord.Embed(title='Інформація', color=settings['color'], description='You chose the category **Information**'))
That's the way I have a code not working
You can't respond to an interaction after it is expired, but you can still send a message to the channel assuming you have the proper permissions.
Also I'd highly recommend calling the Interaction variable interaction rather than iter since it is more readable.
eg.
await interaction.channel.send("interaction expired")
Also is_expired() is a method, so you may need to call it with parentheses.
Im new to python (i learned the basics at a school course),
and for the moment im trying to create simple bots for my discord server.
And for the moment, the bot i really want, is an autodelete bot so that i can say, (for example) delete every message in the ...Channel after 24h. because i really dont wanna do that manually.
I know there are a few good bots that do that,
but for example MEE6 wants me to buy Premium to use the funktion.The other reason why i dont want to use any finished bot is that i really want to understand and learn the code,
i watched many tutorials and tried to put the parts of the scripts that i understood together, but it did not work. I also didnt find a tutorial which explained it to me so that i understood, so now im here and hope that im going to understand it.
I hope there are some ppl to help me. :)
Thanks
-Yami.Code
#bot.event()
async def on_ready(ctx):
while requirement == 1:
await ctx.channel.purge
time.sleep(20)
#the Error is:
line 11, in <module>
#bot.event()
TypeError: event() missing 1 required positional argument: 'coro'
Process finished with exit code 1
You shouldn't call bot.event (remove the parenthesis),
time.sleep is a blocking call, use asyncio.sleep instead (What does "blocking" mean)
on_ready doens't take ctx as an argument, if you want to send a message to a channel you should get the channel object first, and then use the send method
You're missing a parenthesis in your channel.purge method...
import asyncio # if you haven't already
#bot.event
async def on_ready():
channel = bot.get_channel(channel_id) # replace `channel_id` with an actual channel ID
while requirement == 1:
await channel.purge(limit=x) # change `x` accordingly...
await asyncio.sleep(20)
How can a python bot read every message that is sent in a certain channel or read every message that was sent in one using a command like $save 421345187663708161. Thanks in advance, havent been able to find the answer anywhere.
I made this in an on_message Function which scans the message content for "$save" on the beginning, then gets the channel per the given ID.
#client.event
async def on_message(message):
if message.content.startswith("$save"):
splittedcontent = message.content.split()
channel = client.get_channel(splittedcontent[1])
all_messages = channel.history()
What your job now is, is to understand this code, maybe inform about the things that are going on here (like channel.history) and implement this to your code (preferably also with some try/except cases).
I need to make a command to delete every channel (text and voice) in a discord server with discord.py. Can someone provide me with the code to do this. Right now i have this, which only deletes text channels. How do i make it for both at the same time?
#bot.command(name="deleteall")
async def delete_channels(context):
[await channel.delete()
for channel in context.guild.text_channels]
This seems like an oddly destructive command, you should be careful with its use and definitely add permissions checks.
To delete the voice channels, you'd just repeat the code you've already written, but just use the voice_channels list.
e.g.
[await channel.delete() for voiceChannel in context.guild.voice_channels]
#bot.command()
async def delchannels(ctx):
for c in ctx.guild.channels: # iterating through each guild channel
await c.delete()
You can also vary this specifically for text channels or voice channels using:
#bot.command()
async def delchannel(ctx, channel: discord.TextChannel):
await channel.delete()
And alter the argument type for specific channel types: "VoiceChannel" or "CategoryChannel".
So my bot DM's users and asks them questions, but I need to be able to see their response in a channel.
So far I have this:
#bot.event
async def on_message(message):
channel = bot.get_channel(642759168247463937)
await channel.send('message')
but it begins when any message is sent in any channel and it also responds to itself, causing an endless loop of spam.
I'm new to Discord.py so I honestly don't have a clue on how to resolve this issue.
My approach would be to check if the message came from DMs:
#bot.event
async def on_message(message):
if not message.guild:
return
channel = bot.get_channel(642759168247463937)
await channel.send('message')
This works because message.guild is None if the message was sent in DMs. This prevents the spam problem, since the message is not forwarded to the bot's DMs.
However, to avoid the spam in general, it is good practice to avoid responding to bots altogether, unless necessary. Luckily, User and Member objects have an attribute called bot that helps us here:
if message.author.bot:
return
Note that this is done automatically for commands using the ext.commands framework.