This is my first time using the Discord.py library. I have the following code:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
bot = commands.Bot(intents=intents, command_prefix='$')
#client.event
async def on_ready():
print(f'Logged in as {client.user}!')
#bot.command()
async def say(ctx, arg):
await ctx.send(arg)
client.run('MY_TOKEN')
When I say "$say "hello world"" I don't get any error message, but I also don't get any response from the bot. Can someone tell me what the issue is, I'm sure it's a simple mistake. Thanks.
I figured out what I did wrong. I didn't need to include client and bot as separate things. I simply removed client = discord.Client(intents=intents) and the code ran fine (obviously I replaced every instance of 'client' with 'bot').
Related
Trying to make a sorta complicated bot, but before i get started on writing the other code I wanted to just make sure that the bot worked in Discord, and was up and running and responding to a basic command - Send the message "Hello" when someone used the command "!hello".
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
TOKEN = "TOKEN"
#bot.event
async def on_ready():
print(f'Bot connected as {bot.user}')
#bot.command(name='hello')
async def dosomething(ctx):
print(f'Hello command made')
await ctx.send("Hello!")
bot.run(TOKEN)
the on_ready function does work, it outputs the name of the bot whenever it connects, but trying to get it to respond to a simple !hello command does nothing, it doesnt message in the channel and it doesn't print to console. Here's my permissions for the bot as well from Discord Developer Portal - https://imgur.com/a/xrcH1tw
Try adding this at the line above commands.Bot
intents.message_content = True
I came back to creating bots on discord, but this time with Python, it seems I am doing something wrong, and I don't know what it is.
I placed a simple code, which is (command-reply), but the bot does not answer and does nothing about it.
My bot is online, I don't get errors in my terminal, and I am using the correct token.
Here is my code:
import discord
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
#client.event
async def on_message(message):
if message.content == "hello":
await message.channel.send("Hello there!")
client.run('BOT TOKEN')
You didn't enable the message_content intent.
Docs about intents: https://discordpy.readthedocs.io/en/stable/intents.html
i'm making a discord bot for my server, but my codes continue to give me this error. My goal is to have a class with some commands, but I always get the error "discord.ext.commands.errors.CommandNotFound". These are my code:
import discord
from discord.ext import commands
class General_commands(commands.Cog):
def __init__(self, bot):
self.bot = bot
#commands.command()
async def message(self, ctx:commands.context, *, channel: discord.VoiceChannel):
await ctx.channel.send('message')
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix='-', intents=intents)
#bot.event
async def on_ready():
print('Logged in as {0.user}'.format(bot))
bot.add_cog(General_commands(bot))
bot.run(token)
Do you know what I do wrong? If something is unclear you can simply ask :)
I've already tried to use examples saw in github but I didn't menage to solve my problem :(
If I put the command outside the function, with some adjustments it works, but would preferably have it in the class.
Cog loading became asynchronous in 2.0, your code is very outdated. Read the migration guide: https://discordpy.readthedocs.io/en/stable/migrating.html#extension-and-cog-loading-unloading-is-now-asynchronous
ext.commands.Bot.add_cog() must now be awaited.
I'm playing around with the Discord API and noticed that I can't access the content of a message.
This is my code:
import discord
client = discord.Client()
#client.event
async def on_ready():
print(f'Logged in as {client.user}')
#client.event
async def on_message(message):
if 'My Name' in message.author.name:
print(f'Author: {message.author.name}')
print(f'Content: {message.content}')
print(f'Clean_Content: {message.clean_content}')
print(f'System_Content: {message.system_content}')
client.run(TOKEN, bot=False)
Note that the token and my username are kept private in this post for obvious reasons.
This is the output that I get, no matter the message:
Author: My Name
Content:
Clean_Content:
System_Content:
As you can see I have also tried the clean_content and system_content attributes. However, none of them show the actual message. I've also tried to use a bot account and that surprisingly worked, but I want this to work with my own account. Is the problem that Discord does not intent private clients to read messages or did I miss something fundamental?
As Matt mentioned, user bots are not supported. However, your problem may be related to intents instead. You might try using the following lines:
import discord
intents = discord.Intents.all()
client = discord.Client(intents=intents)
See also:
https://docs.pycord.dev/en/master/intents.html#privileged-intents
https://docs.pycord.dev/en/master/intents.html#message-content-intent
You can solve this by enabling the MESSAGE CONTENT INTENT for your bot in the Discord Developer Panel.
import os
TOKEN = os.environ['TOKEN']
import discord
# This example requires the 'message_content' privileged intent to function.
class MyClient(discord.Client):
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')
async def on_message(self, message):
# we do not want the bot to reply to itself
if message.author.id == self.user.id:
return
if message.content.startswith('!hello'):
await message.reply('Hello!', mention_author=True)
intents = discord.Intents.default()
intents.message_content = True
def main():
client = MyClient(intents=intents)
client.run(TOKEN)
if __name__ == '__main__':
main()
This is an example with reply in the documentation
Discord selfbots are no longer supported, you might have to use discord.ext which has support for them still. It seems using discord.py message.content will always be empty.
I have a discord bot with like two simple functions (client.event with client = discord.Client()) and recently I've been trying to work on commands for a music bot, but apparenly the bot doesn't even respond to the simplest commands there are. For example:
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix='!')
#bot.command()
async def hello():
print("Hello") #So when I type !hello in a channel I want "Hello" written in the console as prove that the bot is responding
#client.event
async def on_ready():
print("Online.") #Just simple if the bot is online
The function client.event works of course but the bot.command() function wont react in any way. Can anyone help me here? I am really about to break everything I own.
Try this:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def hello(ctx):
print("Hello")
#bot.event
async def on_ready():
print("Online.")
bot.run("Token")
Firstly, why are you defining client and bot simultaneously? Secondly you don't have bot.run() in your code. Thirdly you have to always give ctx in #bot.command.
You either use discord.Client or commands.Bot, you cannot use both of them. If you take a look at your code, you're decorating the command with bot.command(), but at the end of the file you're running client. If you want commands I suggest you use commands.Bot:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def hello(ctx):
print("Hello")
#bot.event
async def on_ready():
print("Online.")
bot.run("XXX")