on_server_join doesn't respond. discord.py - python

I am trying to get an output when the bot is invited to a server, but for some reason when I try to test my code it doesn't output anything. It doesn't give any errors as well. First I tried to do it from a Cog file:
#commands.Cog.listener()
async def on_server_join(self,server):
print("hello")
print(server.id)
then in the main file:
#client.event
async def on_server_join(server):
print("hello world")
print(server.id)
both doesn't result in any errors and they doesn't output anything when I'm kicking the bot from a server and then adding him back.
Other events, like on_ready and on_member_join are working OK.
Creating a new server and adding the bot there also doesn't trigger the event.

on_server_join was changed to on_guild_join when discord.py migrated to v1.0

Related

How to get a telegram bot work in a channel?

I can't get it to work through properties like command or just content_types. No errors show up.
#dp.message_handler(commands=["start"])
async def start(msg:Message):
if msg.chat.type == ChatType.CHANNEL:
await msg.answer(msg)
I made so bot sent a message to the channel. Still nothing.
P.S It works with getUpdates
In the Dispatcher class there is a decorator called "channel_post_handler".

Unable to send a message to Discord using discord.py

I'm trying to simply create a function that sends a discord message to a specific channel when the bot runs. I want to later call this function in a separate python script.
The code below seems to run fine, and the bot appears online - but it is fails to send the message:
import discord
client = discord.Client()
client.run("ABCDeFGHIjklMNOP.QrSTuV-HIjklMNOP") # the token
#client.event
async def on_ready():
logs_channel = client.get_channel(12345689123456789) # the channel ID
await logs_channel.send("Bot is up and running")
on_ready()
I'm sure I'm missing something basic but I cannot figure out what it is. I've tried a similar function in Node.js and it works fine however, I would prefer the bot to work in python. Discord.py is up to date, python is v3.10.
Any help would be greatly appreciated.
Client.run is a blocking call, meaning that events registered after you call it won't actually be registered. Try calling Client.run after you register the event, as in:
import discord
client = discord.Client()
#client.event
async def on_ready():
logs_channel = client.get_channel(12345689123456789) # the channel ID
await logs_channel.send("Bot is up and running")
client.run("ABCDeFGHIjklMNOP.QrSTuV-HIjklMNOP") # the token
You also probably do not want to be manually calling an event handler. If you want to send a message by calling a function, you probably want to consider writing a separate function to the event handler.

Discord bot command keeps working after i delete the code, and outputs several strings which i deleted

So i'm trying to create a discord bot using python and at the beginning I created a simple !ping command which should output "Pong" in the discord server just to see if things work.
Then I tried to change the output to "server"(ran the code again) and it outputs both strings "Pong", and "server". The more strings I replace Pong with, the more are displayed when using the command
This is the code:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '!')
#client.event
async def on_ready():
print('Bot is ready.')
#client.command()
async def ping(ctx):
await ctx.send('Pong')
Can someone explain, please?
It sounds like you're running multiple instances of the bot. It's not enough just to run the bot again, you also have to close the previous instance. Just close whatever program the bot is running in (Visual Studio Code, Python Shell, CMD) and rerun the version you'd like to have running.

Command not found error in Discord Python code

Whenever I run my discord python code, and test it in the discord chat, it says the ping command is not found even though I defined it in the code.
I've tried to use both Bot and Client and both gave the same error.
import discord
from discord.ext import commands
bot_prefix= "]"
bot = commands.Bot(command_prefix=bot_prefix)
bot.run("*")
#bot.event
async def on_ready():
print("ok")
#bot.event
async def on_message(message):
print(message.content)
#bot.command()
async def ping(ctx):
latency = bot.latency
await ctx.send(latency)
Personal information replaced with "*"
The bot should send a message in the user's channel saying the latency of the bot, but instead I just get an error that says:
"Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "ping" is not found" even though I defined the ping command in the code.
Also, it should be noted that the on_ready event never runs; I never get the print statement in the console log.
Any help is appreciated thanks :)
bot.run must be the last line in your code. Python executes sequentially, so all of the stuff below bot.run isn't called until after the bot is finished running.
Okay I got it fixed!!
Apparently there's an issue with the on_message function, I guess I just skipped over it in the FAQ. Anyone confused about this, just add the line:
await bot.process_commands(message)
into your on_message function. When you define your own on_message function it overrides the original one that passes the message into the commands handler.
Also make sure to use bot.run() at the end of your code, after your function declarations. Simple mistakes, but they're all fixed now :)

How To Create A Purge Command With Discord.py

I can't figure out how to create the purge command in discord.py. This is what I am currently using it creates no errors in the code but the bot doesn't respond to the command.
client = discord.Client()
#client.event
async def on_message(message):
if message.content.startswith('!purge'):
tmp = await client.send_message(message.channel, 'Clearing messages...')
async for msg in client.logs_from(message.channel):
await client.delete_message(msg)
By the looks of it you're using the async branch (please switch to rewrite) of discord.py
For one doing the whole command invocation in on_message thing is not, while it works, a good way of doing it. Try out the commands extension!
Secondly there is already a method for this in async see here
Thirdly the reason why your code isn't working or why it doesn't look like it's working is because you are using client.logs_from with no limit specified so it's downloading every message in that channel and this will take a while to do before it executes the code below it.

Categories

Resources