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")
Related
so i was trying to create my first discord bot, with no experience in Python whatsoever just to learn stuff by doing so and stumbled on an Error (Title) which was answered quiet a lot, but didn't really help me.
Im just gonna share my code:
import discord
from discord.ext import commands
import random
client = commands.Bot(command_prefix="=", intents=discord.Intents.all())
#client.event
async def on_ready():
print("Bot is connected to Discord")
#commands.command()
async def ping(ctx):
await ctx.send("pong")
client.add_command(ping)
#client.command()
async def magic8ball(ctx, *, question):
with open("Discordbot_rank\magicball.txt", "r") as f:
random_responses = f.readlines()
response = random.choice(random_responses)
await ctx.send(response)
client.add_command(magic8ball)
client.run(Token)
I tried to run this multiple times, first without the client.add_command line, but both methods for registering a command didn't work for me. I also turned all options for privileged gateway intents in the discord developer portal on. What am i doing wrong?
Because you have an incorrect decorator for your command ping. It should be #client.command() in your case.
Also, in most of the cases, you don't need to call client.add_command(...) since you use the command() decorator shortcut already.
Only when you have cogs, you will use #commands.command() decorator. Otherwise, all commands in your main py file should have decorator #client.command() in your case.
A little bit of suggestion, since you already uses commands.Bot, you could rename your client variable to bot since it is a little bit ambiguous.
You have to use Slash commands cause user commands are no longer available
Example code using Pycord library
import discord
bot = discord.Bot()
#bot.slash_command()
async def hello(ctx, name: str = None):
name = name or ctx.author.name
await ctx.respond(f"Hello {name}!")
bot.run("token")
I have a discord bot with automod and XP system. I have made discord bots before so I know how to make a command, but now it does not seem to work and I don't know why.
When I say a curse it deletes my message and everything there works fine so the on_message works, but those commands don't work. It does not give any error when I type a command, but the but doesn't respond.
Any help would be appreciated.
Here is my code:
import discord
from discord.ext import commands
# Set up the client and command prefix
intents = discord.Intents.all() # Enable all intents
client = commands.Bot(prefix='$', intents=intents)
#client.event
async def on_ready():
print('Bot ready')
#client.command()
async def ping(ctx):
print("ping")
await ctx.send('pong')
#client.event
async def on_message(message):
# Ignore messages sent by the bot
if message.author == client.user:
return
# Do some XP things
...
# AutoMod things
...
# Process any commands that are sent by the user
await client.process_commands(message)
print("processing commands")
#client.command()
async def leaderboard(ctx):
# do something
...
# Start the bot
client.run('TOKEN')
Also all intents are enabled in discord developer portal
So what I expected to happen is the program would first print Bot ready. Then I would write $ping to discord and the program would print processing commands and then print pong and say pong in discord.
What actually happened is that the program Printed Bot ready. Then I wrote $ping, the program prints processing commands. And then nothing else happens
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
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.
I am trying to make a discord bot with the Discord.py library. The commands with the #client.command() decorator work fine for me, but none of the event ones that I tried work.
#client.event
async def on_member_join(member):
channel = client.get_channel(ChannelId) #I did define channel Id in my code
await channel.send("someone has joined")
#client.event
async def on_member_remove(member):
print("Someone has left")
I would expect this to output to the terminal or in the channel id I put in, but nothing appears, not even an error message.
*I used client. for all functions.
*I am doing this on mac.
Im not quite sure why its working, I do not get any error messages, and I cant seem to find anyone else with this problem.
Thanks in advance
With version >1.5.0 you can do something like this:
import discord
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
#client.event
async def on_member_join(member):
await member.send("Welcome!")
You also need to enable intents in the developer portal in https://discord.com/developers/applications. Bot > Bot > Presence & Server Members Intents > Toggle On
import discord
intents = discord.Intents.all()
discord.member = True
bot = commands.Bot(command_prefix="[",intents = intents)
and you need to go to developer portal --> applications(select your bot)
and under the setting have a bot. Click it under the page has PRESENCE INTENT and SERVER MEMBERS INTENT you need to open it. That will be working.
If you are using discord.py v1.5.0, see the docs for Gateway Intents