Having Trouble With Discord.py Events - python

I have been using Python for bot development, but recently i faced some problems with events that does not trigger when the action happen.
events like : on_member_remove #I tried both# , on_guild_role_delete , on_guild_channel_delete
I want to know why they don't work , if you know why any of them does not work i will appreciate your help
from discord.ext.commands import Bot, Greedy
from discord import user
import discord
from discord.ext import commands
from discord.ext.commands import Bot, has_permissions, MissingPermissions
import asyncio
intents = discord.Intents()
intents.members = True
bot = commands.Bot(command_prefix="/", help_command=None, intents=intents)
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="TEST"))
print('Hello :|')
#bot.event
async def on_member_remove(member):
print("on_member_kick Worked")
#bot.event
async def on_guild_role_delete(role):
print("on_role_delete Worked")
#bot.event
async def on_guild_channel_delete(channel):
print("on_channel_delete Worked")

Oh dear, this can be fixed if you look at the docs
The on_member_kick, on_role_delete and on_channel_delete events simply do not exist.
Possible alternatives are:
on_member_remove (which should've worked according to the docs)
on_guild_channel_delete
on_guild_role_delete
Also, check that you have enabled the intents on the developers page, it may be the cause for the on_member_remove event to not work.

Related

Discord Python bot commands not responding

basically i have no idea why this code is not working, ive tried using on_message and that works so long as i dont include and if statement to filter for the messages with the prefix at the start. so i scrapped that, this code worked a few months ago with a different bot i made and ive ended up scrapping all the other stuff i was doing and bringing it down to the bare basics because i cant figure out why the bot doesnt recognise messages starting with the prefix.
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
TOKEN = "Token_here"
#bot.command(name='tst')
async def test(ctx):
await ctx.send('testt')
#bot.event
async def on_ready():
print ('Successful login as {0.user}'.format(bot))
bot.run(TOKEN)
ive tried print('Test') debugging aswell see below
#bot.command(name='tst')
async def test(ctx):
print('Test")
then in discord typing the !test command still does nothing and the terminal also remains empty other than the on_ready result of
Successful login as botname#0001
i have no idea whats going on honestly
There are a couple of things that could be causing your problems. I fixed the issue by enabling intents for the bot. This also must be done on the developer portal. https://discord.com/developers/applications
intents = discord.Intents().all()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
you also don't need the name parameter in the command decorator. It can just be written as :
#bot.command()
async def test(ctx):
await ctx.send('testt')
Try these changes:
bot= commands.Bot(command_prefix='!!', intents=discord.Intents.all())
#bot.command()
async def test():
await ctx.channel.send("Test successful")

#command decorator not working on discord.py

My discord bot connects just fine, event on_ready is working with no problems. However when I call any of the commands declared with #bot.command, the bot doesn't recognize the message as a command (at least that's what I think it's happening).
Code:
import discord
import pickle
from discord.ext import commands
from discord.ext.commands import bot
token = ""
bot = commands.Bot(command_prefix='!', case_insensitive=True, intents=discord.Intents.default())
#bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
#bot.command()
async def test(ctx):
print('received')
await ctx.send('msg received')
bot.run(token)
That's pretty much it, when I run it the bot connects just fine, but the !test command doesn't work.
Let me know if I didn't add important information, first time asking something here.
EDIT: It's working locally, it doesn't work when it's being hosted on heroku (bot connects and is online, doesn't recognize commands).
Requirements file:
git+https://github.com/Rapptz/discord.py
discord~=1.7.3
setuptools~=60.2.0

Discord bot to send text messages with image from built in command (Python; Can redo in another language)

I've been working on trying to build a discord bot for a server that you could for example do "!publish [text + upload]" this would then send whatever you've typed (plus the uploaded image) from the bot in the current channel. I had had sqiggly code I was doing in Python off a new tutorials but so far nothing has stuck.
What I've got below is an attempt at having it send an image from the bot on command as a start however it hasn't even functioned this far. If anyone would be able to help me fix this up to get the desired result that would be great, I'm happy to switch up languages if that's easier. So far I've got it repeating text from command (See: $hello the bot will send hello! back. I'll be keeping that command for further use but haven't been able to get anything with an image extension). Have tried both file images and url; tried all the options on discord documentation without success.
import discord
import os
from replit import db
from discord.ext import commands
from discord.ext.commands import bot
import asyncio
import datetime as dt
client = discord.Client()
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print('Connected!')
#client.event
async def on_ready():
print('Hello world {0.user}'
.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('hello!')
#bot.command(pass_context=True)
async def test(ctx):
await ctx.send('Working!', file=discord.File('image.png'))
client.run(os.environ['TOKEN'])
First tutorial I followed had me using replit to code this, store data and keep it cloud-base operational even when the computer wasn't active. The bot is functioning at this time just using the $hello command and anything similar. Nothing with images.
First of all you should only use either client or Bot not both, bot is the better option since it has commands and events. here is your code organized using commands.Bot(command_prefix='!')
import discord
import os
from replit import db
from discord.ext import commands
import asyncio
import datetime as dt
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print(f'Connected! {bot.user}')
#bot.event
async def on_message(message):
if message.author == bot.user:
return
#code here
# !hello
#bot.command()
async def hello(ctx):
await ctx.send('Hey!')
# !test
#bot.command()
async def test(ctx):
await ctx.send('Working!', file=discord.File('image.png'))
bot.run(os.environ['TOKEN'])
You have defined two instances and only ran client. That is why !test did not work

on_member_join(member) is never called in discord.py

I am making a discord bot with discord.py on python 3.8.6.
My other functions, such as on_ready and other commands, work. However, on_member_join is never called when a member joins.
from discord.ext import commands
bot = commands.Bot(command_prefix =".")
#bot.event
async def on_ready():
print('logged on salaud')
#bot.event
async def on_member_join(member):
print("test")
bot.run("TOKEN")
Why is on_member_join not being called, and how can I resolve this?
discord.py v1.5 introduces intents
An intent basically allows a bot to subscribe into specific buckets of events.
In order to be able to get server data you need to:
Enable Server Member intents in your application
And Implement discord.Intents at the beginning of your code.
import discord
intents = discord.Intents.all()
bot = discord.Client(intents=intents)
# or
from discord.ext import commands
bot = commands.Bot(command_prefix = ".", intents=intents)
For more information, read about Intents | documentation

Discord.py Priveleged Intents stopping on_message and commands from working

from discord.ext import commands
from discord.ext import tasks
import random
import typing
from discord import Status
from discord import Activity, ActivityType
from discord import Member
from discord.ext.commands import Bot
from asyncio import sleep
intents = discord.Intents()
intents.members = True
intents.presences = True
print(discord.__version__)
bot = commands.Bot(command_prefix='!', intents =intents)
...
...
#bot.event
async def on_ready():
print('hiii, We have logged in as {0.user}'.format(bot))
await bot.change_presence(activity=discord.Game(name="Exploring the archives"))
bot.loop.create_task(status())
#bot.event
async def on_message(message):
if message.author.id == BOT_ID:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello Dad!')
await bot.process_commands(message)
#bot.event
async def on_member_update(before,after):
if before.status != str(after) :
print("{}, #{} has gone {} .".format(after.name,after.id,after.status))
#bot.event
async def on_member_remove(member):
print(f'{member} has left a server.')
#bot.event
async def on_member_join(member):
print(f'{member} has joined a server.')
await member.send('Private message')
#bot.command(pass_context=True)
async def summon(ctx):
await ctx.send ("I have been summoned by the mighty {}, ".format(ctx.message.author.mention) + " bearer of {}. What is your command?".format(ctx.message.author.id))
Hello. I was trying to build a discord Bot and I was mostly successful except from the fact that I couldn't get on_member_join & on_member_update to work (they didn't even seem to register a user entering or leaving the server so I concluded that I lacked some permissions). After a lot of searching I found this in the discord.py documentation and after adding the intents bit at the beggining of my code the on_member_join, on_member_remove & on_member_update worked, but the on_message event and all the commands do not work (i added a print at the beginning of on_message and nothing happened).
After some debugging I found out that the code that stops the commands from responding seems to be ,intents = intents) . However when this is removed, the on_member_join, on_member_remove & on_member_update (understandably) do not trigger.
Any advice?
I'm guessing that using intents = discord.Intents() has all intents set to False.
You can either use intents.messages = True or intents.all().

Categories