Commands don't run in discord.py 2.0 – no errors, but run in discord.py 1.7.3
Question:
I’m trying to understand how migrating from discord.py version 1.7.3 to 2.0 works. In particular, this is test code I’m using:
from discord.ext import commands
with open('token.txt', 'r') as f:
TOKEN = f.read()
bot = commands.Bot(command_prefix='$', help_command=None)
@bot.event
async def on_ready():
print('bot is ready')
@bot.command()
async def test1(ctx):
print('test command')
bot.run(TOKEN)
In discord.py 1.7.3, the bot prints ‘bot is ready’, and I can do the command $test1
.
In discord.py 2.0, the bot prints ‘bot is ready’, but I can’t do the command, and there isn’t any error message in the console when I’m trying to do the command.
Why does this occur, and how can I restore the behaviour of version 1.7.3 in my bot?
Answers:
Use Intents with discord.py
-
Enable Intents
- On Discord Developer Portal
- Select your application
- Click on the Bot section
- And check
MESSAGE CONTENT INTENT
-
Add your intents to the bot
Let’s add the message_content Intent now.
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents, help_command=None)
-
Put it together
The code should look like this now.
import discord
from discord.ext import commands
with open('token.txt', 'r') as f: TOKEN = f.read()
# Intents declaration
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents, help_command=None)
@bot.event
async def on_ready():
print('bot is ready')
# Make sure you have set the name parameter here
@bot.command(name='test1', aliases=['t1'])
async def test1(ctx):
print('test command')
bot.run(TOKEN)
I’m trying to understand how migrating from discord.py version 1.7.3 to 2.0 works. In particular, this is test code I’m using:
from discord.ext import commands
with open('token.txt', 'r') as f:
TOKEN = f.read()
bot = commands.Bot(command_prefix='$', help_command=None)
@bot.event
async def on_ready():
print('bot is ready')
@bot.command()
async def test1(ctx):
print('test command')
bot.run(TOKEN)
In discord.py 1.7.3, the bot prints ‘bot is ready’, and I can do the command $test1
.
In discord.py 2.0, the bot prints ‘bot is ready’, but I can’t do the command, and there isn’t any error message in the console when I’m trying to do the command.
Why does this occur, and how can I restore the behaviour of version 1.7.3 in my bot?
Use Intents with discord.py
-
Enable Intents
- On Discord Developer Portal
- Select your application
- Click on the Bot section
- And check
MESSAGE CONTENT INTENT
-
Add your intents to the bot
Let’s add the message_content Intent now.
import discord from discord.ext import commands intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix='$', intents=intents, help_command=None)
-
Put it together
The code should look like this now.
import discord from discord.ext import commands with open('token.txt', 'r') as f: TOKEN = f.read() # Intents declaration intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix='$', intents=intents, help_command=None) @bot.event async def on_ready(): print('bot is ready') # Make sure you have set the name parameter here @bot.command(name='test1', aliases=['t1']) async def test1(ctx): print('test command') bot.run(TOKEN)