discord.py on_command event not functioning - events

I am hosting my own local bot, however it seems as 'on_command' does not get invoked when other bots in my server are used. Could this be security restrictions? Is there any way to change this or log other bot commands?
#bot.event
async def on_command(ctx):
print(ctx)
Bot is functioning as expected - nothing is printed when a different bot runs a command.

There's no such event that can detect if other bots' commands are being used. The only one that can do something like that is on_message, and you also have to code the checks yourself.

The on_command() event does exist in discord.py, but it is only invoked when a command is used on your bot.
One way to check if a message is sent by a bot is to use the on_message() event, like this:
#bot.event
async def on_message(message):
if message.author.bot:
# Message is sent by a bot!
# ...
The message.author.bot checks if the message's author is a bot.

Related

Discord bot responding to pings that aren't meant for the bot

For example, I want the bot to delete a message that pings the owner of the server.
I'm pretty new to python, I. currently only have the code that makes the bot run
You need to check on each message if the message.author mentions the server owner.
#bot.event
async def on_message(message):
# Check if message has mentions
if message.mentions:
# check if guild owner is in the mention list
if message.guild.owner in message.mentions:
# delete if yes
await message.delete()
Sources
On Message

How do I get my bot to send a message when it is added to a guild? - discord.py

I want my bot to send a message to a specific guild channel whenever it is added or removed from any guild. How do I make it in Discord.py? I am aware of client.event but I am unsure how to use it.
My bots code (sends to channel at the top of server where it has permissions)
#client.event
async def on_guild_join(guild):
for channel in guild.text_channels:
if channel.permissions_for(guild.me).send_messages:
embedHi = discord.Embed(
title="Thanks for adding me!",
description=
f"<:impostor:774673531786625024>I am the Impostor - a bot created by Baz!<:impostor:774673531786625024>\n\n<:noice:751384305464377375>You can join my support server by running $help and you can view all of my commands here as well!<:noice:751384305464377375>\n\n<:patreon:839897502925062165> Feel free to go to https://www.patreon.com/theimpostor to gain access to cool premium commands! <:patreon:839897502925062165>\nIf you join the <:purple:839879572631453696> Hacker Plan <:purple:839879572631453696>, then you will recieve all premium commands, a special role, early access to commands and even work in progress updates!\n:partying_face:Have fun!:partying_face:\n\n\n<:ping:757276110252670986>When you added this bot, it was in version {__version__}<:ping:757276110252670986>",
url="https://www.patreon.com/theimpostor",
colour=discord.Colour.red())
embedHi.set_thumbnail(
url=
"image url"
)
embedHi.set_image(url="image url")
embedHi.set_footer(
text="© Baz - The Impostor - Among Us bot for Discord")
await channel.send(embed=embedHi)
break
#bot.event
async def on_guild_join(guild):
# send your message
Relevant docs

How to send message on event from pc bot is running on? Not from message/channel?

have watchdog file observer. When specific file appears I obtain it and I want to send it into a discord channel.
What commands/events should I look at/use to do this? It's not coming from async discord function. Event is not coming from channel or message, but from obtaining of the file from the PC which bot is runnin on.
Thank you
I did not get the question well, though, if you want to do something when the bot comes online my suggestion is: use the on_ready event. Here is an example:
#client.event
async def on_ready():
await client.change_presence(activity=discord.Game('Hello!'))
print('Connected to bot: {}'.format(client.user.name))
print('Bot ID: {}'.format(client.user.id))

Discord.py bot boost message

Ok, so I want to know how to make a bot send a message to the user who boosted the server. So like for instance somebody boosted the server and the bot sent them a message. The following is an example of what I'm thinking.
Client.event()
async def on_boost():
booster.send(message)
#client.event
async def on_message(message):
if "MessageType.premium_guild" in str(message.type):
await message.channel.send("Thanks for server boost")
use this
I left a short message because I am not good at English. I'm sorry if
I offended you. If you leave a comment, I'll answer it.
Normally when someone boosts, a message is put into the discord service channel of a guild. I assume that you can have an if statement in an on_message event that will trigger if a message is in the format of an boost message + it's author is either the server or None

How do i make a bot check if a message was sent in a certain server? (discord.py)

Not much to it really.
I just have a bot which has a chat filter function which i only want to run on my friends server. However i have the bot in multiple servers. What do i put before the code to make it only run in a certain server?
The most straightforward way would be to just put a conditional in your on_message definition that prevents it from taking action if the message wasn't from the given server.
async def on_message(message):
WHITELISTED_SERVER_ID = '01234567890123456789'
if message.channel.server.id == WHITELISTED_SERVER_ID:
// do whatever the bot does
You can adapt that to however your bot is designed. There are also other concerns (e.g. if the message is from a direct message, message.channel.server won't exist), but that's the gist of it.

Resources