this is how I've already tried getting the message channel that I would presume the discord server uses for this bot, from having used its other functionality:
#client.event
async def on_message(message):
global message_channel
message_channel = message.channel
I want to send a message saying goodbye to users when they go offline using this function
#client.event
async def on_member_update(before, after):
print(str(before.status), str(after.status))
if str(before.status) == "online" and str(after.status) == "offline":
print("someone went offline")
try: await message_channel.send(f'Goodbye, {after.user}')
except: print('failed to send goodbye message')
I'd appreciate either finding a way to make this work (since currently nothing at all prints from the on_member_update() function, or if I could get the channel that the last message was sent to from the bot (meaning this would only be in place once the bot is used at least once)
I think you should make a own channel for the goodbye messages or define a extra channel for it. This would look like this:
#client.event
async def on_message(message):
pass#your code here
#client.event
async def on_member_update(before, after):
print(str(before.status), str(after.status))
if str(before.status) == "online" and str(after.status) == "offline":
channel = cliet.get_channel(channelId)
print("someone went offline")
try:
await channel.send(f'Goodbye, {after.user}')
except:
print('failed to send goodbye message')
If you want to write the goodbye message in the channel the user last wrote in you can simply make a txt with the users and their last channels. You can also do this with json. If you do it in on_message you get the channel that was last typed in by anyone on the server OR in the dms of the bot. So if userXYZ wrote the BOT "Hello World" via DM and then another user userABC went offline the BOT will send userXYZ a Goodbye message via DM.
Related
Im trying to know how to make the bot remember the message a user send $equal message that user will say, and when the user says $message the bot will say the message the user send on $equal message that user will say.
It's really hard to understand what you want to achieve. Please add more description to your question, but even though I think you want to achieve something like this (if not add a further explanation and I'll try to help):
#client.event
async def on_message(message):
if message.author == client.user: #returning when the author of message is bot
return
msg = message.content #getting content of user's message
await message.channel.send(msg) #sending a message user sent
await client.process_commands(message) #proccesing commands (without this your #client.command won't work)
Remember that you have to enable intents.messages.
Discord.py Documentation - on_message() event
This answer is a complement to #RiveN 's answer.
keywords = ["um", "keyboard"]
grab_top = ["top", "t"]
#client.event
async def on_message(msg):
if msg.author == client.user:
return
if msg.content in keywords:
await msg.channel.send('This is a keyword')
if msg.content in grab_top:
await scrape_website()
if msg.content.startswith("~test()"):
await msg.channel.send('This is our test')
inside of discord:
Me: ~test()
Quaint Faint Saint BOT:
This is our test
Me: um
Quaint Faint Saint BOT:
This is a keyword
I have this code:
#client.event
async def on_message(message):
print(message.author)
print(message.content)
this code prints a feed of all of the messages send from channels that the bot has permissions to see. basically i'm trying to write code that does the same with dms.
and I can't figure out how.
To check if a message is a dm, check if the Guild is None.
if message.guild is None:
print(f"DM: {message.content}")
You can also make the bot send DMs to a specific channel using the following -
Just make sure to replace 1234567890 with channel id you want the bot to send the DMs to.
#client.event
async def on_message(message):
if isinstance(message.channel, discord.DMChannel):
emb=discord.Embed(title=message.author, description=message.content)
channel = client.get_channel(1234567890)
await channel.send(embed=emb)```
You could use:
if isinstance(message.channel, discord.DMChannel):
print(message.author)
print(message.content)
As Stijndcl said above, you want to check if the message is sent in a guild, but you also want to check if the bot sends the message.
if message.author == client.user:
return
if not message.guild:
print(f"DM: {message.content}")
If you code your bot further you might end up DM'ing users, and you don't want to log your own messages.
I'm setting up a python TwitchIO chat bot. The getting started example has this:
async def event_message(self, message):
print(message.content)
await self.handle_commands(message)
#commands.command(name='test')
async def my_command(self, ctx):
await ctx.send(f'Hello {ctx.author.name}!')
If the incoming message is a command (e.g. !test), a message is sent back to the channel with ctx.send().
I'd like the option to send a message back to the channel (based on come criteria) whenever any message is received, not just commands. For example:
async def event_message(self, message):
print(message.content)
if SOMETHING:
SEND_MESSAGE_HERE
await self.handle_commands(message)
I can't figure out how to do some type of .send to make that happen. This answer: TwitchIO: How to send a chat message? show this:
chan = bot.get_channel("channelname")
loop = asyncio.get_event_loop()
loop.create_task(chan.send("Send this message"))
I tried that and the bot sent a ton of messages instantly and got timed out for an hour.
So, the question is: How do you send a message back to the channel inside event_message
A command passes you a Context (an instance of of Messageable, which defines send()). A Channel is also a Messageable, thus it has a send(). So if your Bot instance is bot:
await bot.connected_channels[0].send('...')
Will send a message to the first channel the bot is connected to.
This is the answer I came up with:
bot_account_name = "BOT_ACCOUNT_NAME"
async def event_message(self, message):
print(message.content)
if message.author.name.lower() != bot_account_name.lower():
ws = self._ws
await ws.send_privmsg('CHANNEL_NAME', 'Message To Send')
await self.handle_commands(message)
You can do other checks, but it's important to do the check against the bot account user name. If you don't the bot will see its own messages and reply to them creating a loop. This is one of the issues I ran into where it send a bunch in no time and got timed out by twitch
I found an alternative. I replace the message with a command that I invented for the purpose. The name of the command is "z" but it could have been anything
async def event_message(self, message):
if message.echo:
return
message.content = f"#z {message.content}" # add command name before message
await self.handle_commands(message)
#commands.command()
async def z(self, ctx: commands.Context):
print(f"Original message is {ctx.message.content[3:]}\n")
I want to make a bot for discord that will reply to each message using cleverbot. I would like that messages can be without any prefix or command. I will use cleverbot api or selenium for get the cleverbot answer. What is the easiest way to get the last message written in the chat and reply to it?
Something like this:
User: What is your favorite color?
Bot: Green.
You should use the on_message event to listen for a message. Then you can check the content, get your response, and then use channel.send (passing reference=message) to reply.
Below is an example where every time a user says "hi" the bot responds with "hello":
#bot.event # or, if in a cog, #commands.Cog.listener()
async def on_message(message):
if message.author.bot:
return # ignore bots
if message.content == "hi":
await message.channel.send("hello", reference=message)
on_message event is working when a message sent to a channel that your bot can see.
In cog:
#commands.Cog.listener()
async def on_message(self, message):
if not message.author.bot: #checking user is not a bot
if message.content.lower() == "what is your favorite color":
await message.reply("Green. What is your's?")
Not in cog:
#bot.event
async def on_message(message):
if not message.author.bot:
if message.content.lower() == "what is your favorite color":
await message.reply("Green. What is your's?")
#we are handling messages so your bot's commands will not work
#but there is a solution for this
else:
ctx = bot.get_context(message) #getting commands.Context object
await bot.invoke(ctx) #running command if exist
Is there a way to detect when a user (not the bot itself) has sent a message? I understand that the on_message event allows comparisons to an expected command/ response (https://discordpy.readthedocs.io/en/latest/api.html) but for my project, there is no set user response. This makes it so the bot triggers everytime it detects it's own message. How can I fix that?
try this
client = discord.Client()
#bot.event
async def on_message(message):
if message.author == client.user:
return
#then write the code down here so if the person who send message is bot it wont count