Python discord bot commands aren't working - python

import discord
from discord.ext import commands
from discord.ui import Select,View
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_ready():
print('Logged in!')
#bot.command()
async def test(ctx):
await ctx.send("hello")
bot.run("token here")
When I try to use the !test
nothing happens (no message or error)
what is the problem?
also installed library py-cord and discord.py

You're missing the Intents. Create an Intents object, enable message_content (so your commands can be parsed) and it will work.
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
You also need to enable the message_content in the Developer portal.
The message_content intent is on the first page of the docs.
Discord intents.

Related

Discord.py bot can't see message.content from other users in guild [duplicate]

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)

Discord.py bot commands don't respond when using Heroku

I have a pre made Code with Discord.py and hosted using Heroku, it is currently online except that whenever i use a command, it returns nothing, but when running it locally using Notepad++ the commands are working just fine.
main.py
import discord
from discord.ext import commands
token = 'OBVIOUSLY THE TOKEN IS HERE'
prefix = "?"
intents = discord.Intents.default() # or .all() if you ticked all, that is easier
intents.members = True # If you ticked the SERVER MEMBERS INTENT
bot = commands.Bot(command_prefix=prefix, intents=intents) # "Import" the intents
#bot.event
async def on_ready():
print('Internal has connected to Discord!')
await bot.change_presence(activity=discord.Game(name="Discord"))
#bot.command()
async def ping(ctx):
await ctx.send(f'Pong! In {round(bot.latency * 1000)}ms')
#bot.command()
async def about(ctx):
embed = discord.Embed(title='About me!', description=f'Im an Utility bot, that is coded under discord.py\n\n> Statistics\nLatency: `{round(bot.latency * 1000)}`ms', color=0xEE8700)
await ctx.send(embed=embed)
#bot.command()
async def say(ctx, *, content:str):
await ctx.send(content)
bot.run(token)
Versions:
python= 3.8.8
discord.py= 1.7.3
(Adding this as an answer because I assume someone else will have this problem)
In discord.py version 1.7.3, the messages intent, which is required for viewing message content, is set enabled by default. However, in 2.x you need to set it separately.
You can solve the issue by:
Force discord.py version to go back to 1.7.3 in requirements.txt; or
Explicitly enable the intents by doing intents.message_content = True.

Commands don't run in discord.py 2.0 - no errors, but run in discord.py 1.7.3

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)

Discord Bot on_member_join

I need help with a discord Bot,
import discord
client = discord.Client()
#client.event
async def on_member_join(member):
print('new member')
role = discord.utils.get(member.guild.roles, name='Unnamend')
await member.add_roles(role)
print(str(member.roles))
this is my code and if I join I didnt get a message or a role did some one know how I can fix it?
It looks like Intents are missing.
Make sure to turn on all the necessary ones in the Discord Developer Portal for your application.
To implement them to your code you can use the following:
intents = discord.Intents.all() # Imports all the Intents
client = commands.Bot(command_prefix="YourPrefix", intents=intents)
Or in your case:
intents = discord.Intents.all() # Imports all the Intents
client = discord.Client(intents=intents)

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

Categories