My discord bot get this error this will explan what error I got from the message
error image
import os
import discord
client = discord.Client()
#client.event
async def on_ready():
print("I'm in")
print(client.user)
#client.event
async def on_message(message):
if message.author != client.user:
await message.channel.send(message.content[::-1])
my_secret = os.environ['DISCORD_BOT_SECRET']
client.run(my_secret)
Discord bots now require you to pass it a set of intents to run. Intents allow bot developers to "subscribe" to specific events in Discord.
Your discord client constructor takes a parameter of intents
https://discordpy.readthedocs.io/en/stable/api.html?highlight=intents#discord.Intents
You can construct your own object with custom intents, or just use an intents class method to give it all, default or no intents.
Related
I am trying to create slash commands using discord.py but they do not show up when using discord.
I have tried re inviting the bot with applications.commands and giving the bot admin.
The code:
import discord
from discord.ext import commands
import os
import time
from dotenv import load_dotenv
import random
#sorry, global variable
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
client = discord.Client()
bot = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
#bot.command()
async def test(ctx, arg):
await ctx.send(str(arg))
client.run(TOKEN)
#bot.command() makes message commands, not slash commands. You won't see any slash commands if you don't make any...
There's an official example for slash commands that you could look at.
Also, you have both a Client and a Bot. That doesn't make any sense, you can only have one. Choose one, get rid of the other.
Next, your message command test is registered to your Bot, but you're running the Client.
Lastly, you're not passing intents. Make sure you're on the most recent version of dpy.
import discord
intents = discord.Intents(messages=True)
client = discord.Client(command_prefix='!', intents=intents)
import random
import time
import asyncio
TOKEN = "<My token>"
intents = discord.Intents(messages=True)
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("hi"): #whenever i type this
await message.channel.send("Hello") #i dont get this
#client.event
async def on_ready():
print("Running!")
client.run(TOKEN) #Token
thanks for the help,i just need the program to say hello when i say hi,
I am kinda new to making bots soo yeah. I copied this from a youtube vid, but the intents i did by myself. I am using 3.8 version of Python, it runs without errors
This may be because you did not update your intents or in general something wrong with the intents,
Intents on the discord developer portal are in the Bot Section,
If these are enabled then try printing message.content to see if it returns blank or what the user says.
For example, I want the bot to delete a message that pings the owner of the server.
I'm pretty new to python, I. currently only have the code that makes the bot run
You need to check on each message if the message.author mentions the server owner.
#bot.event
async def on_message(message):
# Check if message has mentions
if message.mentions:
# check if guild owner is in the mention list
if message.guild.owner in message.mentions:
# delete if yes
await message.delete()
Sources
On Message
I am making a discord bot in python and I want it to send a message to only the person who typed in a command without sending it to anyone else.
Any ideas?
See here: https://discordpy.readthedocs.io/en/rewrite/ext/commands/commands.html#converters
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix='!')
#bot.command(name='your_command')
async def DM(user: discord.User, message="Hi"):
await user.send(message)
I am trying to do a repeat command with this code:
import discord
from discord.ext import commands
bot = discord.Client()
client = commands.Bot(command_prefix='V!')
#client.command(name='repeat')
async def _repeat(ctx, arg):
await ctx.send(arg)
bot.run('TOKEN')
but when sending a message with a command, the bot doesnt respond neither with the wanted message, nor an error that would imply something is not right. i am also very new to programming so it may be something dumb that i do not know. Any help is appreciated.
If you examine your code carefully, you'll see that you are assigning the command to the client object, but running the bot object. You need to do client.run("<TOKEN>") as another commenter suggested.
You also don't need bot = discord.Client() at all. discord.Client is a parent class with less abilities. I encourage you to rename client to bot though.
from discord.ext import commands
bot = commands.Bot(command_prefix='V!')
#bot.command(name='repeat')
async def _repeat(ctx, arg):
await ctx.send(arg)
bot.run('TOKEN')
Notice now there's no import discord or discord.Client anywhere.
See: What are the differences between Bot and Client?