LoginSignup
10
10

More than 3 years have passed since last update.

discord.pyいろんなメモ

Last updated at Posted at 2020-08-01

初めに

記事を書くのは初めてなので、分かりにくい点などがあるかもしれませんがコメントなどで指摘していただけるとありがたいです。

本記事はdiscord.pyのメモ書きとなっています。
本記事に書いてあることで分からない所があれば質問してください。答えられる範囲で回答します。

python 3.8.3
discord.py 1.3.4

本記事の基本

import discord

client = discord.Client()

# ここに記述

client.run("token")

役職付与/剥奪

@client.event
async def on_message(message):
    if message.content == "/add":
        role = discord.utils.get(message.guild.roles, name="ロール名") # サーバー内の「ロール名」というロールを取得
        await message.author.add_roles(role) # 上記で取得したロールを付与
    if message.content == "/remove":
        role = discord.utils.get(message.guild.roles, name="ロール名")
        await message.author.remove_roles(role) # 上記で取得したロールを剥奪

ファイルを送信

@client.event
async def on_message(message):
    if message.content == "/send":
        await message.channel.send(file=discord.file("ファイルのパス"))

送信場所がDMかサーバーか判定

@client.event
async def on_message(message):
    if message.content == "/send":
        if message.guild: # 送信場所がDMだったら
            await message.author.send("DM") # メッセージ送信者に送信
        if not message.guild: # 送信場所がサーバーだったら
            await message.channel.send("サーバー") # 送信したチャンネルに送信

embed(埋め込みメッセージ)の書き方

@client.event
async def on_message(message):
    if message.content == "/embed":
        embed = discord.Embed(title="タイトル", description="説明", colour=0xff0000)
        embed.add_field(name="フィールド名", value="フィールド値", inline=False)
        embed.set_thumbnail(url=message.author.avatar_url)
        embed.set_image(url="https://vignette.wikia.nocookie.net/hitlerparody/images/a/af/Discord_Logo.png/revision/latest?cb=20181215111027&path-prefix=es")
        embed.set_author(name="著者名", icon_url="https://image.winudf.com/v2/image1/Y29tLmRpc2NvcmRfaWNvbl8xNTU0MDY5NjU3XzAyMQ/icon.png?w=170&fakeurl=1")
        embed.set_footer(text="フッター", icon_url=client.user.avatar_url)
        await message.channel.send(embed=embed)

上記のコードの場合こんな感じに表示されます。
sdf.png

基本構造はこんな感じですが、embedを簡単に生成できるサイトもあるので紹介しておきます。
Discord Embed Generator

いろんな変更

だいたいリファレンスに書いてあるのでその情報をこちらに記述します。

チャンネル編集

@client.event
async def on_message(message):
    if message.content == "/edit":
        await message.channel.edit(name="テキストチャンネル") # チャンネル名を変更
        await message.channel.edit(topic="トピック") # チャンネルトピックを変更
        await message.channel.edit(nsfw=True) # NSFWチャンネルに変更(Falseで無効)

ユーザー編集

@client.event
async def on_message(message):
    if message.content == "/edit":
        await message.author.edit(nick="ニックネーム") # ニックネームを変更
        await message.author.edit(mute=True) # ボイスチャンネルでミュートに変更(Falseで解除)
        await message.author.edit(voice_channel=None) # ボイスチャンネルから切断

サーバー編集

@client.event
async def on_message(message):
    if message.content == "/edit":
        await message.guild.edit(name="サーバー") # サーバー名を変更
        await message.guild.edit(description="説明") # サーバーの説明を変更
        await message.guild.edit(region="europe") # サーバー地域を変更

指定できる地域一覧
https://discordpy.readthedocs.io/en/latest/api.html#discord.VoiceRegion

チャンネルを作成

@client.event
async def on_message(message):
    if message.content == "/create":
        ch = await message.channel.category.create_text_channel(name="ch") # 名前「ch」のテキストチャンネルを作成
        await message.channel.send(ch.mention + " を作成しました。")

いろんな数を取得

@client.event
async def on_message(message):
    if message.content == "/servers":
        await message.channel.send(len(client.guilds)) # BOTが参加しているサーバー数
        await message.channel.send(len(message.guild.members)) # サーバー内のメンバー数を取得

こちらのサイトにその他のものが載っています

その他情報

メッセージは文の途中で \n を入れると改行することができます。

最後に

BOTの開発者が集まるサーバーでDiscord Bot Portal JPというサーバーがあります。
こちらのサーバーは質問もすることができるので調べても分からないことがあれば利用すると解決できるかもしれません。
※質問の際は最低限調べてから質問してください

discord.py公式
discord.py非公式日本鯖
Python専門鯖

10
10
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
10
10