【问题标题】:Discord.py NameError: name 'ctx' is not definedDiscord.py NameError:未定义名称'ctx'
【发布时间】:2022-07-20 03:59:49
【问题描述】:

我正在学习 python,我正在开发一个不和谐的机器人。我想制作一个自动角色系统,但它显示未定义“ctx”。代码如下:

import discord
from discord.ext import commands


intents = discord.Intents().all()
client = commands.Bot(command_prefix="?", intents=intents)
client.remove_command('help')

server = ctx.message.server


perms = discord.Permissions(administrator=True)


@client.event
async def on_ready():
    client.create_role(server, name='Unverified', permissions=perms)


@client.event
async def on_member_join(member):
    role = discord.utils.get(member.guild.roles, name='Unverified')
    await member.add_roles(role)

【问题讨论】:

标签: python discord.py


【解决方案1】:

这行不通,因为 ctx 没有在任何地方定义检查此代码以了解 ctx 值在命令中的工作方式还有其他方法可以做到,但我是这样做的:

@client.command()
async def hello(ctx):
    await ctx.send('hello')

这是一个使用 ctx 的基本命令,如果你想将你的值放入“@client.command”中,你必须使用它

【讨论】:

    【解决方案2】:

    正如@PranavHosangadi 在 cmets 中所说,ctx 需要定义。它不是在导入discord 模块时被继承/包含的对象。

    【讨论】:

      猜你喜欢
      • 2021-01-31
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2021-04-15
      • 2019-01-26
      • 2021-10-05
      • 2017-08-16
      相关资源
      最近更新 更多