简体   繁体   中英

I want to receive information from the user from discord, but I don't know what to do. ( discord.py )

I want to receive information from the user from discord, but I don't know what to do.

I want to make a class to input data

if user write,make [name] [data], bot generate class A, A(name, data)

The following is the code I made. What should I do?

Ps. command_prefix is not working properly. What should I do with this?

`


import discord, asyncio
import char # class file
from discord.ext import commands

intents=discord.Intents.all()
client = discord.Client(intents=intents) 

bot = commands.Bot(command_prefix='!',intents=intents)

@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.online, activity=discord.Game("Game"))


@client.event
async def on_message(message):
    if message.content == "test":
        await message.channel.send ("{} | {}, Hello".format(message.author, message.author.mention))
        await message.author.send ("{} | {}, User, Hello".format(message.author, message.author.mention))
    
    if message.content =="!help":
        await message.channel.send ("hello, I'm bot 0.0.1 Alpha") 
    async def new_class(ctx,user:discord.user,context1,context2):
        global char_num
        globals()['char_{}'.format(char_num)]=char(name=context1,Sffter=context2,username=ctx.message.author.name)
        char_num+=1
        await ctx.message.channel.send ("done", context1,"!")

client.run('-')

`

I advice to not using on message for making commands what I do advice:

import discord
from discord.ext import commands
    
@bot.command(name="name here if you want a different one than the function name", description="describe it here", hidden=False) #set hidden to True to hide it in the help
async def mycommand(ctx, argument1, argument2):
      '''A longer description of the command


    Usage example:
    !mycommand hi 1
     '''
    await ctx.send(f"Got {argument1} and {argument2}")

if you will use the two ways together then add after this line

await message.channel.send ("hello, I'm bot 0.0.1 Alpha") this:

   else:
        await bot.process_commands(message)

if you want to make help command u should first remove the default one by editing this line bot = commands.Bot(command_prefix=',',intents=intents) to:

bot = commands.Bot(command_prefix='!',intents=intents,help_command=None)

overall code should look like

import discord, asyncio
import char # class file
from discord.ext import commands

intents=discord.Intents.all()
client = discord.Client(intents=intents) 

bot = commands.Bot(command_prefix='!',intents=intents,help_command=None)

@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.online, activity=discord.Game("Game"))


@client.event
async def on_message(message):
    async def new_class(ctx,user:discord.user,context1,context2):
        global char_num
        globals()['char_{}'.format(char_num)]=char(name=context1,Sffter=context2,username=ctx.message.author.name)
        char_num+=1
        await ctx.message.channel.send ("done", context1,"!")

@bot.command()
async def make(ctx,name,data):
    #do whatever u want with the name and data premeter
    pass
@bot.command()
async def help(ctx):
    await ctx.send("hello, I'm bot 0.0.1 Alpha")
@bot.command()
async def test(ctx):
        await ctx.send ("{} | {}, Hello".format(ctx.author, ctx.author.mention))


client.run('-')

and yeah if u want to know what is ctx ctx is context which is default premeter and have some methods like send,author and more

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