'Im learning to make a bot using python. What do I do so that I can create a new channel using the commands?

Thanks for the previous people that helped me for the last question What Im trying to do is make a different file for a function which is when you type "!Create" + (The name I want the channel to be) it creates a new channel with a specified name and I want that file to be able called back in the main channel

Code 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'))

Code 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'))


Solution 1:[1]

From the comments, I assume you want to create a channel via a command. To accomplish this you need to consider a few things:

If you want to use commands you have to change client = discord.Client() to client = commands.Bot(command_prefix="YourPrefix") and import commands from discord.ext:

from discord.ext import commands

client = commands.Bot(command_prefix="!") # Example prefix

First: You have to set up conditions, which have to be fulfilled. You want to be able to give the channel its own name? Assume this as the required argument:

@client.command()
async def cchannel(ctx, *, name):
  • We said that name is a required argument.
  • * is used so that we can also inpute names like t e s t and not only test.

Second: You need to define the guild where you want to create the channel. To do that we use:

guild = ctx.message.guild

Finally we want to create a text channel. To do this we use:

@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
  • While creating the channel we said that the name should be our required argument name.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dominik