1
0

More than 1 year has passed since last update.

コマンドを打つとチャンネルを作ってくれるdiscordbotをPythonで作ろう #3

Last updated at Posted at 2021-12-27

前回の続きです。
しばらく更新していませんでした。すみません。

ボイスチャンネルを作成する

全体のコードです。

if message.content.startswith('.vc'):
    msg=message.content.split()
    try:
        if len(msg[1]) > 0:
            await message.channel.send(f'{message.author.mention}がボイスチャンネルを作成しました。'
                                       f'\nチャンネル名は、{msg[1]} です。')
            await message.guild.create_voice_channel(name=msg[1])
        # 2021/12/27追記
        # 同じカテゴリに作りたい場合、(name=msg[1], category=message.channel.category)としてください


    except:
        await message.channel.send(f':thinking: ,{message.author.mention} チャンネル名を入力してください。')

解説

これもテキストチャンネルとほぼ同じです。
>>await message.channel.send(f'{message.author.mention}がボイスチャンネルを作成しました…

(成功時)
(メッセージ送信者)がボイスチャンネルを作成しました。。
チャンネル名は、(チャンネル名) です。


(失敗時)
:thinking: ,(メッセージ送信者) チャンネル名を入力してください。

>> await message.guild.create_voice_channel(name=msg[1])

メッセージを発したギルドに新しくvcチャットが作成されます。

カテゴリーの作成

コードです

if message.content.startswith('.cat'):
    try:
        msg=message.content.split()
        if len(msg[1]) > 0:
            await message.channel.send(f'{message.author.mention}がカテゴリーを作成しました。'
                                       f'\nチャンネル名は、{msg[1]} です。')
            await message.guild.create_category(name=msg[1])

    except:
        await message.channel.send(f':thinking: ,{message.author.mention} カテゴリー名を入力してください。')

解説(?)

これも今までと同様に msg[1] がカテゴリ名になります。

解説とまでは言えませんがこれで一番下にカテゴリが作られます。

最終的なコード

# coding=utf-8
import discord

client = discord.Client()


@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('-' * 10)


# メッセージ内で:
@client.event
async def on_message(message: discord.Message):
    if message.content.startswith('.txt'):
        msg = message.content.split()

        try:
            if len(msg[1]) > 0:
                await message.channel.send(f'{message.author.mention}がテキストチャンネルを作成しました。'
                                           f'\nチャンネル名は、{msg[1]} です。')
                await message.guild.create_text_channel(name=msg[1])
                # 2021/12/27追記
                # 同じカテゴリに作りたい場合、(name=msg[1], category=message.channel.category)としてください

        except:
            await message.channel.send(f':thinking: ,{message.author.mention} チャンネル名を入力してください。')

    if message.content.startswith('.vc'):
        msg = message.content.split()
        print(msg)

        try:
            if len(msg[1]) > 0:
                await message.channel.send(f'{message.author.mention}がボイスチャンネルを作成しました。'
                                           f'\nチャンネル名は、{msg[1]} です。')
                await message.guild.create_voice_channel(name=msg[1])
                # 2021/12/27追記
                # 同じカテゴリに作りたい場合、(name=msg[1], category=message.channel.category)としてください

        except:
            await message.channel.send(f':thinking: ,{message.author.mention} チャンネル名を入力してください。')

    if message.content.startswith('.cat'):
        msg = message.content.split()
        try:
            if len(msg[1]) > 0:
                await message.channel.send(f'{message.author.mention}がカテゴリーを作成しました。'
                                           f'\nチャンネル名は、{msg[1]} です。')
                await message.guild.create_category(name=msg[1])

        except:
            await message.channel.send(f':thinking: ,{message.author.mention} カテゴリー名を入力してください。')

client.run('your_bot_token')

次はおまけを書こうかと・・

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0