我正在学习用python制作一个机器人.我该怎么做才能使用命令创建一个新频道



感谢前面帮助我回答最后一个问题的人我想做的是为一个函数制作一个不同的文件,当你键入";!创建"+(我希望频道的名称(它创建了一个具有指定名称的新频道,我希望该文件能够在主频道中回调

代码1:

import os
import discord
import createchannel
client = discord.Client()
@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))
commands = {
    '$Hello' : 'Hello! write "$Help" to find more about the CazB0T ',
    '$Help' : 'Hi again! The CazB0T is still in beta stages...the only 5 commands are $Hello, $Help, $Test1, $Test2, and $Test3',
    '$Test1' : 'This is Test1',
    '$Test2' : 'This is Test2',
    '$Test3' : 'This is Test3'
}
createchannel()
my_secret = os.environ['token']
@client.event
async def on_message(message):
    if message.author == client.user: 
        return
    for key, value in commands.items():
        if message.content.startswith(key):
            await message.channel.send(value)

client.run(os.getenv('token'))

代码2:

import discord
import os
client = discord.Client()
@client.event
async def on_chancreate(channel):
  channelname = input()
  if channel.content == "!create" + channelname:
    channel = await channel.create_text_channel(channelname)
client.run(os.getenv('token'))

根据注释,我假设您希望通过命令创建一个通道。要做到这一点,你需要考虑几件事:

如果要使用commands,则必须将client = discord.Client()更改为client = commands.Bot(command_prefix="YourPrefix"),并从discord.ext:导入commands

from discord.ext import commands
client = commands.Bot(command_prefix="!") # Example prefix

第一:您必须设置条件,这些条件必须得到满足。你想给频道起自己的名字吗?假设这是必需的参数:

@client.command()
async def cchannel(ctx, *, name):
  • 我们说过name是必需的参数
  • 使用*,这样我们也可以输入像t e s t这样的名称,而不仅仅是test

第二:您需要定义要创建通道的公会。为此,我们使用:

guild = ctx.message.guild

最后,我们想要创建一个文本通道为此,我们使用:

@client.command()
async def channel(ctx, *, name):
    guild = ctx.message.guild # Get the guild
    await guild.create_text_channel(name=name) # Create a text channel based on the input
    await ctx.send(f"Created a new channel with the name `{name}`") # Optional
  • 在创建通道时,我们说名称应该是我们所需的参数name

最新更新