简体   繁体   中英

Discord.py NameError: name 'ctx' is not defined

i'm learning python and i'm developing a discord bot. I want to make an auto-role system but it's showing that 'ctx' is not defined. Here's the code:

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)

This can't work because ctx is not defined anywhere check this code to understand how ctx value work in commands there is other ways to do it but me I do it like that :

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

This was a basic command with use ctx if you wanna to put your value you have to use it into the "@client.command"

As @PranavHosangadi said in the comments, ctx needs to be defined. It is not an object that is inherited/included when importing the discord module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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