I am using the newest version of discord py and python. I wanted to make a custom bot with slash commands in cogs. For now my bot already has some cogs command but now slash commands thee commands work perfectly fine. I looked up some things and came up with this but as you can guess its not working. so far i wrote this code in my cog:
import discord
from discord import app_commands
from discord.ext import commands
class Slash(commands.Cog):
def __init__(self, client: commands.Bot):
self.client = client
#commands.Cog.listener()
async def on_ready(self):
print("Slash cog loaded")
#commands.command()
async def sync(self, ctx) -> None:
fmt = await ctx.client.tree.sync(guild=ctx.guild)
await ctx.send(f"Synced {len(fmt)} commands.")
#app_commands.command(name="slash", description="test slash command")
async def ping(self, interaction: discord.Interaction):
bot_latency = round(self.client.latency * 1000)
await interaction.response.send_message(f"Pong! {bot_latency} ms.")
async def setup(client):
await client.add_cog(Slash(client), guilds=[discord.Object(id="HEREISMYSERVERID")])
I also wrote this code in my main file:
import discord
from discord.ext import commands, tasks
from itertools import cycle
import os
import asyncio
import json
client = commands.Bot(command_prefix="!", intents=discord.Intents.all(), application_id=MYAPPPLICATIONID)
bot_status = cycle(["Secret Things...","cooking"])
#tasks.loop(seconds=18000)
async def change_status():
await client.change_presence(activity=discord.Game(next(bot_status)))
#client.event
async def on_ready():
print("Success: Bot is connected to Discord")
change_status.start()
async def load():
for filename in os.listdir("./cogs"):
if filename.endswith("py"):
await client.load_extension(f"cogs.{filename[:-3]}")
async def main():
async with client:
await load()
await client.start("MYTOKEN")
asyncio.run(main())```
I have also a bunch of normal cogs these work perfectly fine. also in my console i see that this slash cog was loaded. But when i try to use !sync nothing happens can someone please help me with this
The docs for add_cog say this about the guilds kwarg:
If the cog is an application command group, then this would be the guilds where the cog group would be added to. If not given then it becomes a global command instead.
Your cog is not an application command group (it's a regular cog), so this doesn't do anything. As a result, your slash command is registered as a global one. This means that syncing to the guild (sync(guild=...)) does nothing, as the command isn't registered to that guild.
You can either use app_commands.guilds to add one specific command to a guild, or create a GroupCog instead of a regular cog to do it for the entire cog.
Related
I'm developing discord bot with discord.py==2.1.0.
I use cog to write the main function that I wanna use, but I found when the whole bot is wrapped in async function and called by asyncio.run(), my terminal won't show any error message when there is any runtime error in my cog script.
Here is the example application. I stored my bot token in environment variable.
bot.py
import os
import discord
from discord.ext import commands
import asyncio
token = os.environ["BOT_TOKEN"]
class Bot(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
description = "bot example."
super().__init__(
command_prefix=commands.when_mentioned_or('!'),
intents=intents,
description=description
)
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')
bot = Bot()
async def load_extensions():
for f in os.listdir("./cogs"):
if f.endswith(".py"):
await bot.load_extension("cogs." + f[:-3])
async def main():
async with bot:
await load_extensions()
await bot.start(token)
asyncio.run(main())
./cogs/my_cog.py
from discord.ext import commands
class Test(commands.Cog):
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_ready(self):
print("Ready")
#commands.command()
async def command(self, ctx):
test_error_message() # An runtime error example
print("Command")
async def setup(client):
await client.add_cog(Test(client))
Command that I run in terminal to start the bot.
python bot.py
When I type !command in the discord channel, there is no error message showing in the terminal, but there is no "Command" printed out so I'm sure the code stopped at the line I called test_error_message()
I expected that it should show error message normally, but I cannot find useful reference to make it work :(
There is one reason I need to use asyncio, I have a task loop to run in the bot, like the code below.
from discord.ext import tasks
#tasks.loop(seconds=10)
async def avatar_update():
# code here
async def main():
async with bot:
avatar_update.start()
await load_extensions()
await bot.start(token)
I would happy to know if there are some great practice to handle error in this situation.
Thanks!
Client.start() doesn't configure logging, so if you want to use that then you have to do it yourself (there's setup_logging to add a basic config). run() configures logging for you.
For more info, read the docs. https://discordpy.readthedocs.io/en/stable/logging.html?highlight=logging
I'm having a problem that after restarting the bot, the slash commands doesn't update, it stays the one I've made first, this is my simple code:
import discord
from discord.ext import commands
from discord_slash import cog_ext, SlashContext
class Slash(commands.Cog):
def __init__(self, bot):
self.bot = bot
#cog_ext.cog_slash(name="Soporte",description="Comando para ver las opciones de soporte")
async def _support(self, ctx: SlashContext):
await ctx.channel.send("✈️ Telegram: #Isaac_Sanz\n💬 Discord: ElmerKao_#0058 \n🌐 Página Web: https://nakiri.x10.mx/")
def setup(bot):
bot.add_cog(Slash(bot))
Here is a prove that everything is running as it should be:
But when I enter discord to run the command it only shows the test one I did before:
Could someone explain what is happening and any solution?
Found the issue, seems that you need to load the cogs before the something like this:
cogs = ["cogs.roles","cogs.users","cogs.moderation","cogs.ticket-es","cogs.ticket-en"]
bot = ComponentsBot("!", help_command=None,intents=intents)
#BOT STARTUP
#bot.event
#When bot starts
async def on_ready():
print('Bot {0.user} funcionando perfectamente'.format(bot))
print("-----------------------------------------")
print("ID: " + str(bot.user.id))
print("Version de Discord: " + str(discord.__version__))
print(f'Actualmente en {len(bot.guilds)} servidores!')
for server in bot.guilds:
print(server.name)
print("-----------------------------------------")
#COGS
#This loads the cogs like in line 16
print("Cargando cogs . . .")
for cog in cogs:
try:
bot.load_extension(cog)
print(cog + " ha sido cargada.")
except Exception as e:
print(e)
print("\n")
Here is how it should look like:
Here is the link of the question where I found the answer:
Link
They're sort of in the middle of adding slash commands to discord.py but you can see a few examples in https://gist.github.com/Rapptz/c4324f17a80c94776832430007ad40e6 You seem to be using discord_slash, which I have not used.
I'm not quite sure how to do it in cogs, because I have mine in groups but basically what I'm doing is a combination of #bot.tree.command() in the main bot file and a few groups in separate files.
So here's my main file
import discord
import simplegeneralgroup
from config import TOKEN
MY_GUILD = discord.Object(id=1234567890)
class MyBot(discord.ext.commands.Bot):
async def on_ready(self):
await self.tree.sync(guild=MY_GUILD)
bot: discord.ext.commands.Bot = MyBot
#bot.tree.command(guild=MY_GUILD)
async def slash(interaction: discord.Interaction, number: int, string: str):
await interaction.response.send_message(f'Modify {number=} {string=}', ephemeral=True)
bot.tree.add_command(simplegeneralgroup.Generalgroup(bot), guild=MY_GUILD)
if __name__ == "__main__":
bot.run(TOKEN)
and then the simplegeneralgroup file
import discord
from discord import app_commands as apc
class Generalgroup(apc.Group):
"""Manage general commands"""
def __init__(self, bot: discord.ext.commands.Bot):
super().__init__()
self.bot = bot
#apc.command()
async def hello(self, interaction: discord.Interaction):
await interaction.response.send_message('Hello')
#apc.command()
async def version(self, interaction: discord.Interaction):
"""tells you what version of the bot software is running."""
await interaction.response.send_message('This is an untested test version')
There should be three commands: /slash, which will prompt the user for a number and string, /generalgroup hello, and /generalgroup version
The main documentation for this stuff is https://discordpy.readthedocs.io/en/master/interactions/api.html?highlight=dropdown#decorators but the main "how to" is that you've gotta make a "tree", attach commands to that tree, and sync your tree for the commands to show up. discord.ext.Bot makes its own tree, which is why I'm using that instead of client, which I think doesn't make a tree by default.
If you specify the guilds, the commands sync takes place instantly, but if you don't specify the guild, I think it takes an hour to update or something like that, so specify the guild until you're ready for deployment.
How do you add more than one command/function to python.py ?
This is my code:
import os
import discord
client = discord.Client()
#client.event
async def on_ready():
print(f"{client.user} is online")
#client.event
async def on_message(message):
if message.content.startswith("https://"):
await message.delete()
client.run(my_secret)
To add more commands,
Here is the syntax for a new command
#client.command(name="wave")
async def command_name(ctx,user: discord.Member):
await ctx.send(f"{ctx.author} has waved to {user.mention}")
import discord
from discord.ext import commands
Having at least these two imports is a must. Now you should be prepared to create more commands,
as a template
#client.command()
async def command_name(arguments) :
#rest of your code here
and if the command is inside a cog
#commands.command()
async def command_name(self, other arguments) :
#rest of your code here
I suggest you to read the docs though, blindly follow won't help
I've been working on trying to build a discord bot for a server that you could for example do "!publish [text + upload]" this would then send whatever you've typed (plus the uploaded image) from the bot in the current channel. I had had sqiggly code I was doing in Python off a new tutorials but so far nothing has stuck.
What I've got below is an attempt at having it send an image from the bot on command as a start however it hasn't even functioned this far. If anyone would be able to help me fix this up to get the desired result that would be great, I'm happy to switch up languages if that's easier. So far I've got it repeating text from command (See: $hello the bot will send hello! back. I'll be keeping that command for further use but haven't been able to get anything with an image extension). Have tried both file images and url; tried all the options on discord documentation without success.
import discord
import os
from replit import db
from discord.ext import commands
from discord.ext.commands import bot
import asyncio
import datetime as dt
client = discord.Client()
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print('Connected!')
#client.event
async def on_ready():
print('Hello world {0.user}'
.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('hello!')
#bot.command(pass_context=True)
async def test(ctx):
await ctx.send('Working!', file=discord.File('image.png'))
client.run(os.environ['TOKEN'])
First tutorial I followed had me using replit to code this, store data and keep it cloud-base operational even when the computer wasn't active. The bot is functioning at this time just using the $hello command and anything similar. Nothing with images.
First of all you should only use either client or Bot not both, bot is the better option since it has commands and events. here is your code organized using commands.Bot(command_prefix='!')
import discord
import os
from replit import db
from discord.ext import commands
import asyncio
import datetime as dt
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print(f'Connected! {bot.user}')
#bot.event
async def on_message(message):
if message.author == bot.user:
return
#code here
# !hello
#bot.command()
async def hello(ctx):
await ctx.send('Hey!')
# !test
#bot.command()
async def test(ctx):
await ctx.send('Working!', file=discord.File('image.png'))
bot.run(os.environ['TOKEN'])
You have defined two instances and only ran client. That is why !test did not work
I am dabbling in the creation of a discord bot for my private discord server and I have run into an issue.
I have three functions that load, unload and reload extensions in the form of cogs. The creation of the load and unload commands are totally fine but I am having trouble with the reload command.
In the interest of not repeating code, I want to call unload(extension) and load(extension inside of the reload(extension) command, however, I have not yet been able to figure out how to do so.
Here is my code:
import discord
from discord.ext import commands
import os
from settings import BOT_TOKEN
client = commands.Bot(command_prefix=(".", "!", "?", "-"))
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.idle)
print("Discord_Bot is ready")
#client.command()
async def load(ctx, extension):
client.load_extension("cogs.{0}".format(extension))
#client.command()
async def unload(ctx, extension):
client.unload_extension("cogs.{0}".format(extension))
#client.command()
async def reload(ctx, extension):
await ctx.invoke(client.get_command("unload({0}".format(extension)))
await ctx.invoke(client.get_command("load({0})".format(extension)))
# Load Cogs on Boot
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
client.load_extension("cogs.{0}".format(filename[:-3]))
client.run(BOT_TOKEN)
I also have an example_cog.py I use to test the functionality of the load, unload and reload commands. There are no commands in this file, just the basics needed to function as a cog.
example_cog.py
import discord
from discord.ext import commands
class Example_Cog(commands.Cog):
def __init__(self, client):
self.client = client
def setup(client):
client.add_cog(Example_Cog(client))
When I use the bot on my private discord server and try to reload, it does not work. I have read the documentation and I cannot figure out how to pass arguments into the bot.get_command() function. I would vastly appreciate help on this issue.
I have tried many different ways of using the bot.get_command() function but none of them work. These include:
await ctx.invoke(client.get_command("unload {0}".format(extension)))
await ctx.invoke(client.get_command("unload({0})".format(extension)))
Thanks, Ben
You need to pass name of command in string type. Example:
#bot.event
async def on_ready():
# call command without args
await bot.get_command('examplecommand').callback()
# call command with args
await bot.get_command('exampleArgsCommand').callback(ctx, message)
#bot.command()
async def examplecommand():
pass
#bot.command()
async def exampleArgsCommand(ctx, message):
pass