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.
Related
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.
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 trying to build a discord bot but every time I try to run it it just says this 'error bot not defined' and I'm not sure what causes it as I've defined in in a file I've called bot. The first string of code is the one which vs is having a problem with the second one is the 'bot' file.
import discord
from discord.ext import commands
async def setup(bot):
await bot.load_extension(help_cog(bot))
await bot.load_extension(music_cog(bot))
class help_cog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.help_message = '''
General Commands:
/help - displays all the available commands to help all of you Blookteers
/p - finds a song that you (the blooketeers) have chosen and will play it in your current channel
/q - displays the current music queue chosen by the Blooketeers themselfs
/skip - skips the current song being played which was chosen by one of the Blooketeers
/clear - Stop the music and clears the whole queue
/leave - When the blooketeers are tired of music this command will ofrce the bot to leave the current voice channel
/pause - pauses the current song or resumes if it's aleady paused
/resume - resumes playing the current song
'''
self.text_channel_text = []
async def on_ready(self):
for guild in self.bot.guilds:
for channel in guild.text_channels:
self.text_channel_text.append(channel)
await self.send_to_all(self.help_message)
async def send_to_all(self, msg):
for text_channel in self.text_channel_text:
await text_channel.send(msg)
#command.command(name='help', help='Displays all the avalable commands to help out all of you blooketeers!')
async def help(self, ctx):
await ctx.send(self.help_message)
import hikari
import lightbulb
import discord
from discord.ext import commands
import os
from help_cog import help_cog
from music_cog import music_cog
intents=discord.Intents.all()
TOKEN = os.getenv('DISCORD_TOKEN')
bot = lightbulb.BotApp(token=TOKEN, intents=intents, prefix="!")
bot.remove_command('help')
async def setup(bot):
await bot.add_cog(help_cog(bot))
await bot.add_cog(music_cog(bot))
bot.run()
I've tried to change the bot value but everything I've tried has failed as well as trying to use discord.bot instead of lightbulb but I may of done either of these wrong so don't rule them out.
I started learning python today and made a Discord bot. I have a few problems:
If message.author == was used in the on_message, but the bot continued to reply to itself.
Afterwards, a new bot was created with a new token and the code didn't work.
I searched a lot on this site and Google. I didn't find any solution. It's okay to modify my code. Everything is from the internet, so it can be a mess. Please help me.
import discord
import asyncio
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_ready():
print('Loggend in Bot: ', bot.user.name)
print('Bot id: ', bot.user.id)
print('connection was succesful!')
print('=' * 30)
#client.event
async def on_message(message) :
if on_message.content.startswith('!์๋ขฐ'):
msg = on_message.channel.content[3:]
embed = discord.Embed(title = "๋ธ๋ฆฌํ", description = msg, color = 0x62c1cc)
embed.set_thumbnail(url="https://i.imgur.com/UDJYlV3.png")
embed.set_footer(text="C0de")
await on_message.channel.send("์๋ก์ด ์๋ขฐ๊ฐ ๋ค์ด์์ต๋๋ค", embed=embed)
await client.process_commands(message)
client.run("My bot's token.")
Your code was messy, but it should work now. I included comments to let you know how everything works. I think the good starting point to making your own bot is reading documentation. Especially Quickstart that shows you a simple example with explanation.
Write !example or hello to see how it works.
import discord
import asyncio
from discord.ext import commands
# you created 'client' and 'bot' and used them randomly. Create one and use it for everything:
client = commands.Bot(command_prefix="!") # you can change commands prefix here
#client.event
async def on_ready(): # this will run everytime bot is started
print('Logged as:', client.user)
print('ID:', client.user.id)
print('=' * 30)
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'): # you can change what your bot should react to
await message.channel.send("Hello! (This is not a command. It will run everytime a user sends a message and it starts with `hello`).")
await client.process_commands(message)
#client.command()
async def example(ctx): # you can run this command by sending command name and prefix before it, (e.g. !example)
await ctx.send("Hey! This is an example command.")
client.run("YOUR TOKEN HERE")
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