Hey everyone! I’m working on a Discord bot to practice Python, but I’m stuck. When I try to run it, I get an error about missing ‘intents’. Here’s what I’ve got:
# discord_bot.py
import random
def process_command(cmd):
cmd = cmd.lower()
if cmd == 'hi':
return 'Hello!'
elif cmd == 'dice':
return f'You rolled a {random.randint(1,6)}'
elif cmd == '!info':
return 'This bot is under construction.'
# bot_runner.py
import discord
import discord_bot
async def reply(msg, user_input, private):
try:
response = discord_bot.process_command(user_input)
if private:
await msg.author.send(response)
else:
await msg.channel.send(response)
except Exception as e:
print(f'Error: {e}')
def start_bot():
TOKEN = 'YOUR_BOT_TOKEN_HERE'
bot = discord.Client()
@bot.event
async def on_ready():
print(f'{bot.user} is online!')
@bot.event
async def on_message(msg):
if msg.author == bot.user:
return
content = str(msg.content)
if content.startswith('!'):
await reply(msg, content[1:], private=True)
else:
await reply(msg, content, private=False)
bot.run(TOKEN)
# main.py
import bot_runner
if __name__ == '__main__':
bot_runner.start_bot()
I ran into a similar issue when I was starting with Discord bots. The error you’re seeing is because Discord now requires you to specify intents when initializing the client. It’s a security measure they implemented.
To fix this, you need to import and set up intents. In your bot_runner.py, add this near the top:
Then, when you create your client, pass the intents:
bot = discord.Client(intents=intents)
This should resolve the error. Just make sure you’re using a recent version of discord.py (2.0+). If you’re still having trouble, double-check your Discord Developer Portal to ensure you’ve enabled the necessary intents for your bot there too.
Hope this helps! Let me know if you need any clarification.
I encountered this exact issue recently. The problem stems from a recent Discord API update that mandates explicit declaration of intents for bots. To resolve it, you’ll need to modify your bot_runner.py file.
This should resolve the ‘missing intents’ error. Make sure you’re using discord.py version 2.0 or higher. Also, don’t forget to enable the necessary intents in your Discord Developer Portal for the bot. If you’re still facing issues after these changes, double-check your token and permissions.