如何在Discordpy的同一python文件中添加多个命令

时间:2020-09-19 05:15:04

标签: python discord discord.py

我一直在关注Robotic Nation的Discord.py教程,并且一直想知道如何在单个python文件中添加多个命令。 这是我的代码:

import discord 

from discord.ext import commands

client = commands.Bot(command_prefix=".")

@client.command()
async def repeat(ctx, *args):
    for arg in args:
        await ctx.send(arg)

@client.command()
async def server(ctx):
    name = str(ctx.guild.name)
    description = str(ctx.guild.description)
    owner = str(ctx.guild.owner)
    id = str(ctx.guild.id)
    region = str(ctx.guild.region)
    memberCount = str(ctx.guild.member_count)
    icon = str(ctx.guild.icon_url)

    embed = discord.Embed(
        title = name + " Server Information",
        description = description,
        color=discord.Color.dark_red()
    )
    embed.set_thumbnail(url=icon)
    embed.add_field(name="Owner", value=owner, inline=True)
    embed.add_field(name="Server ID", value=id, inline=True)
    embed.add_field(name="Region", value=region, inline=True)
    embed.add_field(name="Member Count", value=memberCount, inline=True)

    await ctx.send(embed=embed)

client.run('[Bot Token]')

(在此处链接到视频播放列表:https://www.youtube.com/playlist?list=PLJXEdhN0Tc3LRT716enS1LcY4OF8vg1VA

我尝试添加一个client.command(),然后在其下添加新命令,但是无论何时运行该文件,.repeat命令都可以正常工作,但是当我尝试使用{{1} }终端会说

.server

但是,如果两个命令都位于单独的文件中,则它们可以正常工作,但是我希望能够使用两个命令而不必停止运行一个文件,然后启动另一个来使用它。

我对使用Discordpy开发机器人很陌生,感谢您的帮助!

使用的库:discord.py 1.4.1 Python解释器:Python 3.8.5 [32位]

1 个答案:

答案 0 :(得分:0)

就像那样,由于它们都是函数,因此可以将它们堆叠起来。

import discord 

from discord.ext import commands

client = commands.Bot(command_prefix=".")


#________________________________________
@client.command()
async def yourcommandnamehere(ctx):
    await ctx.send("random text")
#_______________________________________________

@client.command()
async def repeat(ctx, *args):
    for arg in args:
        await ctx.send(arg)

@client.command()
async def server(ctx):
    name = str(ctx.guild.name)
    description = str(ctx.guild.description)
    owner = str(ctx.guild.owner)
    id = str(ctx.guild.id)
    region = str(ctx.guild.region)
    memberCount = str(ctx.guild.member_count)
    icon = str(ctx.guild.icon_url)

    embed = discord.Embed(
        title = name + " Server Information",
        description = description,
        color=discord.Color.dark_red()
    )
    embed.set_thumbnail(url=icon)
    embed.add_field(name="Owner", value=owner, inline=True)
    embed.add_field(name="Server ID", value=id, inline=True)
    embed.add_field(name="Region", value=region, inline=True)
    embed.add_field(name="Member Count", value=memberCount, inline=True)

    await ctx.send(embed=embed)

client.run('[Bot Token]')```