简体   繁体   English

使用 python 的 Discord 斜杠命令

[英]Discord slash commands using python

I have been looking at stackoverflow posts and so many places but still can't find the answer to this question that works for me.我一直在查看 stackoverflow 帖子和很多地方,但仍然找不到适合我的问题的答案。 How do I make discord python slash commands?如何制作 discord python 斜杠命令? Following this post: https://stackoverflow.com/questions/71165431/how-do-i-make-a-working-slash-command-in-discord-py#:~:text=import%20discord%20from%20discord.ext%20import%20commands%20create%20you,ids%20in%20which%20the%20slash%20command%20will%20appear .在这篇文章之后: https ://stackoverflow.com/questions/71165431/how-do-i-make-a-working-slash-command-in-discord-py#:~:text=import%20discord%20from%20discord .ext%20import%20commands%20create%20you,ids%20in%20which%20the%20slash%20command%20will%20appear I got the error:我得到了错误:

Traceback (most recent call last): File "/home/container/bot.py", line 3, in bot = discord.Bot(command_prefix=":") AttributeError: module 'discord' has no attribute 'Bot'回溯(最后一次调用):文件“/home/container/bot.py”,第 3 行,在 bot = discord.Bot(command_prefix=":") AttributeError: 模块 'discord' 没有属性 'Bot'

With this code:使用此代码:

import discord
from discord.ext import commands
bot = discord.Bot(command_prefix="!")
@bot.slash_command(name="first_slash") #Add the guild ids in which the slash command will appear. If it should be in all, remove the argument, but note that it will take some time (up to an hour) to register the command if it's for all guilds.
async def first_slash(ctx): 
    await ctx.respond("You executed the slash command!")

I tried replacing "bot = discord.Bot" with "bot = commands.Bot" but that didn't work either我尝试用“bot = commands.Bot”替换“bot = discord.Bot”,但这也不起作用

The only code I found had no errors was:我发现唯一没有错误的代码是:

import discord
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext

bot = commands.Bot(command_prefix="!")
slash = SlashCommand(bot)
@slash.slash(name="test")
async def _test(ctx: SlashContext):
    await ctx.send("Hello World!")

But no slash commands appeared on discord但是discord上没有出现斜杠命令

Discord.py is no more updated. Discord.py 不再更新。

If you want to use a more recent version of the discord module, you have to install pycord https://guide.pycord.dev/installation如果你想使用更新版本的 discord 模块,你必须安装 pycord https://guide.pycord.dev/installation

To do this, first you have to uninstall the discord.py module using: pip uninstall discord.py And then install pycord using: pip install py-cord To do this, first you have to uninstall the discord.py module using: pip uninstall discord.py And then install pycord using: pip install py-cord

Your script will work like that:您的脚本将像这样工作:

import discord
from discord.ext import commands

bot = discord.Bot(debug_guilds=["YOUR TEST GUILD'S ID HERE"])

@bot.slash_command(name="first_slash")
async def first_slash(ctx):
    await ctx.respond("You executed the slash command!")

You have this error because you use discord's client and not discord.ext's.你有这个错误是因为你使用了 discord 的客户端而不是 discord.ext 的。

bot = commands.Bot(command_prefix='!',intents=discord.Intents.all()) #gets all intents for the bot to work

Next up, slash won't be used (variable and event).接下来,将不使用斜杠(变量和事件)。 Replace to this event:替换为此事件:

@bot.hybrid_command(put same args as the actual code)

After this is done, update the Command Tree and it should appear no problem on Discord.完成后,更新命令树,在 Discord 上应该没有问题。 Link here: https://discordpy.readthedocs.io/en/stable/interactions/api.html#commandtree链接在这里: https ://discordpy.readthedocs.io/en/stable/interactions/api.html#commandtree

Hope it helped.希望它有所帮助。 :D :D

EDIT: try this, It's your code but I modified it (if it works well for me, it will work well for you):编辑:试试这个,这是你的代码,但我修改了它(如果它适合我,它也会适合你):

main.py:主要文件:

import os
import discord
from discord.ext import commands
bot = discord.Bot(command_prefix="!",intents=discord.Intents.all()) #intents are required depending on what you wanna do with your bot

@bot.hybrid_commands(name="first_slash")
async def first_slash(ctx): 
   await ctx.send("You executed the slash command!") #respond no longer works, so i changed it to send

@bot.event
async def on_ready():
   await bot.sync() #sync the command tree
   print("Bot is ready and online")

try:
   bot.run("Put your token here")
except discord.HTTPException as e:
   if e.status == 429: #rate limited: too much connexions
      os.system("restarter.py")
      os.system("kill 1")

restarter.py:重启器.py:

from time import sleep
from os import system
sleep(7)
system("main.py") #change main.py to whatever your main script is called

Now it should appear up to an hour after sync.现在它应该在同步后最多出现一个小时。 Hope this helps more.希望这有助于更多。 (Little precision: if you use Pycord, the code will be different) (小精:如果用Pycord,代码会不一样)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM