Discord.py message spamming - discord.py

my message is spamming, I only want to send it 1 time. how to fix?
#client.event async def on_message(message): if 'happy birthday' in message.content.lower(): await message.channel.send('Happy Birthday! 🎈🎉')

Because it's an on_message event and it detects also the bot's messages and bot is sending a message including happy birthday.
If you don't want it spamming, you can check if author is a bot account with message.author.bot.
#client.event
async def on_message(message):
if message.author.bot:
return
if 'happy birthday' in message.content.lower():
await message.channel.send('Happy Birthday! 🎈🎉')

You are creating an infinite loop, when the bot sends ”Happy Birthday!” ”happy birthday” is in the lowercase version of the message. So the bot reads happy birthday and sends a message, Happy Birthday!. Which the bot detects as a new message, the new message (when converted to lowercase) contains happy birthday so the bot sends a message.
There are two ways to fix this, you can change the content of the message to not contain happy birthday
Or you could check if the message is sent from anyone but the bot using message.author message docs page

Related

Save every message in a channel to a txt file discord.py

I want to make a word chaining bot on discord, and I want it to save all the messages in the channel so I can check for duplicates, is this possible?
Of course this is possible. And it is extremely easy to do.
import discord
client = discord.Client()
log = open("log.txt", "a")
def add_message(msg):
log.write(msg)
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
add_message(message) # You can check the channel with message.channel.id
client.run('your token here')
This should work
Edit: If your concern is to check every message against every new message, as other said, switch to a database.
MySql is very easy to setup (compared to PostgreSQL) and connect to Python, and you can check for duplicates very easily.

storing messages with a discord bot

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')

Discord.py How to make the bot check every new message?

I'm using Discord.py how can I make the bot execute some operations when a user post a new message?
I highly recommend reading through the docs first before coming here to ask a question. However, you can use a client event and on_message to check every new message, for example:
#client.event
async def on_message(message):
if message.content == "Hi":
await message.channel.send("Hello!")

How do I get a bot to forward its DMs

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.

How can I make a bot change it's presence (listening, watching, playing, etc...) displayed on discord under their name?

I get nothing showing up that there is an error, but it does not show up in discord. Could someone show me what is wrong?
async def change_status(self):
await client.change_presence(game=Game(name = " ", type = 3))
I would like for the bot to have "listening" or "watching" show up on discord under it's name.
Okay, Update:
(pretty sure this is rewrite)
I figure some people will look this up over time so here it is.
On the discord.py discord server I looked around through #help and found a place where it said the correct answers they just needed to be edited slightly. Here is the answer for it:
await client.change_presence(activity=discord.Game(name="a game"))
This will set the bot to "Playing."
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="a movie"))
This will set it to "Watching" status.
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="a song"))
This will set it to a "Listening" status.
You need the bot to specify when to change the status.
try #client.event this changes the status as the bot comes online.
to change listening to streaming or to watching try changing 2 ( 1,2,3,4 )
As far as i know to use the streaming status feature you need to link your twitch account with your code.
#client.event
async def on_ready():
await client.change_presence(game=Game(name='What ever in here',type = 2))
print('Ready')

Resources