Combining commands and events - discord.py

I am building a discord bot that I want to be able to interact both with commands and emojis. Here is the code so far:
import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
bot = commands.Bot(command_prefix='$')
client = discord.Client()
#bot.command(name='repeat', help='help me to understand bots')
async def test(ctx, *args):
for arg in args:
await ctx.send(arg)
#client.event
async def on_raw_reaction_add(payload):
print("someone reacted to something")
print(payload)
bot.run(TOKEN)
client.run(TOKEN)
My intuition is that the last two lines are where this goes wrong, although I don't understand the run(TOKEN) feature to have a good understanding of why this happens, or how to fix it.

To further develop this you need to fully understand what you've done and have a plan for what you want to do. I will first direct you to the discord python documentation Where you can see everything discord.py has to offer. This should probably be your first stop when you have questions.
I see you have import discord and from discord.ext import commands... discord.py has two ways to approach a bot, which you have:
client = discord.Client() hails from import discord, and
bot = commands.Bot() hails from import discord.ext.
You need to choose whether to use discord.Client() or discord.Bot() because the use cases are entirely different. I suggest the latter, as it will allow your bot to do more, in a cleaner fashion.
Your next step would be to change any # decorators to whichever variable you choose and keep that consistent throughout your code. Then, you need to remove whichever .run(TOKEN) you won't use. So, if I were you, I would read the documentation which I linked to you so you understand more than a YouTube tutorial would teach.
As for a revision to your code, it would look like this:
import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
bot = commands.Bot(command_prefix='$')
#bot.command(name='repeat', help='help me to understand bots')
async def test(ctx, *args):
for arg in args:
await ctx.send(arg)
#bot.event
async def on_raw_reaction_add(payload):
print("someone reacted to something")
print(payload)
bot.run(TOKEN)

Related

my discord bot made with python is not working

here is my code:
import discord
import asyncio
from discord.ext import commands
TOKEN="secret"
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="?", intents = intents)
#bot.event
async def on_ready():
print(f'{bot.user} successfully logged in!')
#bot.event
async def on_message(message):
if message.author == bot.user:
return
await bot.process_commands(message)
#bot.command()
async def spam(ctx, message, *, amount:int):
await ctx.send("Starting Spam...")
new_amount = amount+1
for i in range(1, new_amount):
await ctx.send(message)
await asyncio.sleep(0.5)
await ctx.send("Spam Completed!")
try:
bot.run(TOKEN, bot = False)
except discord.HTTPException as e:
if e.status == 429:
print("The Discord servers denied the connection for making too many requests")
else:
raise e
my bot does go online, however, when I use the spam command, it doesn't work.
the code doesn't give any errors. so i am wondering what the problem is.
any help will be appreciated.
You don't have the message_content intent set to True. First page of docs shows you to need to enable the message_content intent. This allows your bot to read message content; and therefore actually be able to respond to commands.
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="?", intents=intents)
You will also need to enable the message content intent in the Discord Developer Portal as well. Under your application, go to "Bot", and then scroll down to "Privileged Gateway Intents" and then enable the message content one.
According to discord.py docs:
"With the API change requiring bot authors to specify intents, some intents were restricted further and require more manual steps. These intents are called privileged intents."
So basically what you need to do is go to the "Bot" section within your application in the discord developers portal and under the heading Privileged Gateway Intents you need to enable message content intents etc. based on what your needs are. Also this needs to be done through code as well.
In your case:
intents = discord.Intents.default()
needs to be replaced with
intents = discord.Intents(messages=True,message_content=True)
or simply
intents = discord.Intents.all()
Feel free to comment down any queries regarding this solution.

How do I make it so that ctx is not of type 'interaction' (nextcord slash commands)?

I'm trying to create a music bot using nextcord slash commands and interactions. The command isn't fully finished yet, as I am having trouble getting it to even join the voice channel. I don't exactly know how interactions work yet but I'm assuming it's a similar concept as ctx. Below is my music.py cog:
import nextcord
from nextcord.ext import commands
from nextcord import Interaction
class Music(commands.Cog):
def __init__(self, client):
self.client = client
guild_ids = ["Guild Ids Go Here"]
#slash commands go under here
#nextcord.slash_command(name="play", description="plays music in vc", guild_ids = guild_ids)
async def play(self, interaction : Interaction, query: str):
channel = interaction.author.voice.channel #ERROR IS HERE
try:
await channel.connect()
await interaction.response.send_message("The bot has joined vc.")
except:
await interaction.response.send_message("Failed to find voice channel.")
def setup(client):
client.add_cog(Music(client))
I'm getting an error that says "'Interaction' object has no attribute 'author'. It occurs on line 15 in 'play' when it says 'channel = interaction.author.voice.channel'. I think this means that this isn't the right way to go about getting the author's voice channel. If this is the case, is there a better, working method?
In nextcord interaction, message author is interaction.user,
channel is interaction.channel.
You also can send interaction message by interaction.send instead
of interaction.response.send_message. It's much shorter and easier
to read.
If you want to send a normal message without using interaction, try
interaction.channel.send. It's similar to ctx.channel.send in application command

I am trying to make my bot be able to respond to a command that starts with a certain set of characters instead of having an exact command

I am trying to make my bot be able to respond to a command that starts with a certain set of characters instead of having an exact command. I figured out how to use it with client.event but I can't figure out how to do it using client.command.
As an example let's say I want it to send a message that says "Hello!" when the command ".hi" is sent. I want it to work even if a user sends ".hii" or maybe ".hi!". I can get this to work by using the startswith command. Below is my working client.event code and my broken client.command code:
client.event code:
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.content.lower().startswith('.hi'):
await message.channel.send('Hello!')
client.command code:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.', case_insensitive=True)
#client.command()
async def hi(message):
if message.content.startswith('hi'):
print('Hello!')
If you are using the command, you could specify the aliases of the command like this:
#client.command(aliases=['hii', 'hi!'])
async def hi(ctx):
print('Hello!')
https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=aliases#discord.ext.commands.Command.aliases
Also your command is broken because you need to pass in ctx as a parameter for a command, not message.
Your #client.command() won't work because you never passed in ctx, by the way, to send a message you might want to use await ctx.send("Hello!").
The "full command" would look something like this, though its not really what you want as you would have to type .hi hi
#client.command()
async def hi(ctx, message):
if message.startswith('hi'):
await ctx.send('Hello!')
If you actually want to do that you should try using your #client.event (with on_message, basically the one you already have) as I dont think that you can do that unless you use aliases (Which is not a good option though):
#client.command(aliases=['hi!', 'hiiiiii', 'hello'])
async def hi(ctx):
await ctx.send('Hello!')
In conclusion, if you really want to do something like this, use your exsistent #client.event with on_message
(Just as #moinierer3000 said)

How to send DM to command author?

I am trying to make a bot that sends the user who entered the command a DM, but I don't get an error code in PyCharm or a DM in Discord when I enter the command. Am I doing something wrong?
#client.command(aliases=['manage'])
async def oversee(message):
await message.author.send('Hello World!')
According to the discord.py documentation, when you create a command using the commands framework, your first argument must be ctx:
#client.command(aliases=['manage'])
async def oversee(ctx):
await ctx.author.send('Hello World!')
Your function would work as intended if it was a on_message event:
#client.event
async def on_message(message):
if message.content.startswith("!manage") or message.content.startswith("!oversee"):
await message.author.send('Hello World!')
However, I don't recommand using on_message events to create commands, using the commands framework will be way easier and more efficient.

Bot wont properly print member_count

So I'm new to Python and I decided to take the plunge and make a Discord bot for personal use in my server. I like the idea of having full control over what features my bot will have so I'm slowly building the bot. Currently I want my bot to show the current number of members in the server when called with a command
import discord
from discord.ext import commands
#botbot.command()
async def server(ctx):
guild = discord.Guild.member_count
await ctx.send(guild)
I know I'm most likely way off with my code here.
While the bot is sending a message into the chat, its formatted as:
<property object at 0x046036C0>
While I would like to have it say something like "This server has {some number} members."
Any advice is really appreciated!
Thank you for your time.
Edit: botbot is the name for my bot, just so thats clear.
discord.Guild is the class. You want to get the guild the command is in from the invocation context:
#botbot.command()
async def server(ctx):
await ctx.send(f"This server has {ctx.guild.member_count} members.")
The method member_count is not really what you want in this occasion, the guild object has a list called members, so it's as easy as getting the length of that list like so:
#botbot.command()
async def server(ctx):
guild = len(discord.guild.members)
await ctx.send(guild)
EDIT: And indeed you have a typo with using "Guild" instead of "guild"

Resources