无法在discord.py中的斜杠命令中访问公会ID。

huangapple go评论66阅读模式
英文:

Cannot access Guild id in slash commands in discord.py

问题

抱歉,你的代码中出现了一个错误。要解决这个问题,你需要将 guild 定义为一个参数,然后将其传递给你的命令函数。以下是修复后的代码片段:

import discord
from discord import app_commands
from credentials import TOKEN
from functions.dispatch_c import dispatch
from functions.ga_c import ga
from functions.nation_c import nation

intents = discord.Intents.default()
intents.message_content = True
intents.guilds = True
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)

@client.event
async def on_ready():
    print(f"We have logged in as {client.user}")

@client.event
async def on_ready():
    for guild in client.guilds:
        await tree.sync(guild=discord.Object(id=guild.id))
        print("Tree Registered")

@tree.command(name='nation', description='Sends information about a particular nation')
async def nation_command(interaction, nation_name: str, guild: discord.Guild):
    await nation(interaction, nation_name)

@tree.command(name='ga', description='To see information about the General Assembly')
async def ga_command(interaction, proposal_id: int, guild: discord.Guild):
    await ga(interaction, proposal_id)

@tree.command(name='dispatch', description='To see information about a dispatch')
async def dispatch_command(interaction, dispatch_link: str, guild: discord.Guild):
    await dispatch(interaction, dispatch_link)

client.run(TOKEN)

现在,每个命令函数都接受一个名为 guild 的参数,并且在命令注册时将 guild 参数传递给了命令。这应该解决你遇到的 NameError 问题。

英文:

So I'm making a slash commands bot and I'm trying to access the guild id in order to sync the commands but its not working. it gives me a NameError and that apparently guild id isnt defined, ive tried making it a global variable but it doesnt work either. I looked everywhere including stack overflow answer but couldn't find a solution for my type of code

import discord
from discord import app_commands
from credentials import TOKEN
from functions.dispatch_c import dispatch
from functions.ga_c import ga
from functions.nation_c import nation


intents = discord.Intents.default()
intents.message_content = True
intents.guilds = True
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)


@client.event
async def on_ready():
    print(f"We have logged in as {client.user}")

@client.event
async def on_ready():
    for guild in client.guilds:
        await tree.sync(guild=discord.Object(id=guild.id))
        print("Tree Registered")

@tree.command(name = 'nation', description='Sends information about a particular nation', guild=discord.Object(id=guild.id))

async def nation_command(interaction, nation_name: str):
    await nation(interaction, nation_name)

@tree.command(name='ga', description='To see information about the General Assembly', guild=discord.Object(id=guild.id))

async def ga_command(interaction, proposal_id: int):
    await ga(interaction, proposal_id)

@tree.command(name='dispatch', description='To see information about a dispatch', guild=discord.Object(id=guild.id))

async def dispatch_command(interaction, dispatch_link: str):
    await dispatch(interaction, dispatch_link)
    
client.run(TOKEN)

Error:

Traceback (most recent call last): 

File "/home/container/bot.py", line 26, in <module> @tree.command(name = 'nation', description='Sends information about a particular nation', guild=discord.Object(id=guild.id)) 

NameError: name 'guild' is not defined

答案1

得分: 0

不是非常有帮助,但您可以删除guild=guild以使命令在所有服务器上运行,因为这似乎是您尝试实现的目标。

要实现这一点,您必须在await tree.sync(guild=discord.Object(id=guild.id))以及所有@tree.command上都删除它。此外,您需要删除on_ready()中的for循环。

这是修复后的代码:

@client.event
async def on_ready():
    await client.wait_until_ready()
    print(f"We have logged in as {client.user}")
    await tree.sync()

@tree.command(name='nation', description='Sends information about a particular nation')
async def nation_command(interaction, nation_name: str):
    await nation(interaction, nation_name)

@tree.command(name='ga', description='To see information about the General Assembly')
async def ga_command(interaction, proposal_id: int):
    await ga(interaction, proposal_id)

@tree.command(name='dispatch', description='To see information about a dispatch')
async def dispatch_command(interaction, dispatch_link: str):
    await dispatch(interaction, dispatch_link)

client.run(TOKEN)
英文:

Not really helpful but you can remove the guild=guild to have the command work on all servers, as it seems like that's what you were trying to accomplish.

to achieve that you must remove it on both await tree.sync(guild=discord.Object(id=guild.id)) and on all @tree.command you have. Also, You need to remove the for loop at on_ready()

here is the fixed code:

@client.event
async def on_ready():
    await client.wait_until_ready()
    print(f"We have logged in as {client.user}")
    await tree.sync()

@tree.command(name = 'nation', description='Sends information about a particular nation')
async def nation_command(interaction, nation_name: str):
    await nation(interaction, nation_name)

@tree.command(name='ga', description='To see information about the General Assembly')
async def ga_command(interaction, proposal_id: int):
    await ga(interaction, proposal_id)

@tree.command(name='dispatch', description='To see information about a dispatch')
async def dispatch_command(interaction, dispatch_link: str):
    await dispatch(interaction, dispatch_link)
    
client.run(TOKEN)

huangapple
  • 本文由 发表于 2023年6月19日 17:52:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定