How to send message on event from pc bot is running on? Not from message/channel? - discord.py

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

Related

discord.py on_command event not functioning

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.

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

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 can I send a message to every server the bot is in (discord.py)

So I want my bot to send a message in every server my bot is in but it will send a message in only a channel name like
Example:
//broadcast hello
And the bot searches for the channel name general
And the bot sends a message to that channel and it will continue sending "hello" to other servers my bot is in.
So can anyone give me an example? Because of I do not know how. So I ask here for help
The best way to do this is to loop through every server the bot is in, then loop through each channel in each server, testing if that channel's name is #general, if so, send a message to that channel. You can use bot.guilds to get a list of all servers a bot is in, then use guild.channels to get all channels in a server, then you can use channel.name to check the name of the channel. Here's the full command:
#bot.command()
#commands.has_permissions(administrator=True)
async def broadcast(ctx, message):
for guild in bot.guilds:
for channel in guild.channels:
if(channel.name == 'general'):
await channel.send(message)
bot.run(token_here)

Cant get discord.py bot to disconnect from voice chat

I have gotten the connect to work fine, but when trying to get the bot to disconnect, it will print that it has disconnected and will just sit their in chat with no errors. Here's the code:
#client.command(pass_context=True)
async def join(ctx):
channel = ctx.message.author.voice.channel
await channel.connect()
print('connected')
#client.command(pass_context=True)
async def disconnect(ctx):
server = ctx.message.guild.voice_client
await server.disconnect()
print ('disconnected')
Thanks in advance for any help.
You may want to try using server.disconnect(force=True) (documentation) as sometimes disconnecting may be blocked if you don't force it.

Resources