12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

discord.py でボイスチャンネルの入退出をテキストチャンネルに通知するコード

Last updated at Posted at 2019-05-13
import discord
from datetime import datetime, timedelta

client = discord.Client()

@client.event
async def on_voice_state_update(member, before, after): 
    if member.guild.id == 監視するサーバーid and (before.channel != after.channel):
        now = datetime.utcnow() + timedelta(hours=9)
        alert_channel = client.get_channel(通知させたいテキストチャンネルid)
        if before.channel is None: 
            msg = f'{now:%m/%d-%H:%M}{member.name}{after.channel.name} に参加しました。'
            await alert_channel.send(msg)
        elif after.channel is None: 
            msg = f'{now:%m/%d-%H:%M}{member.name}{before.channel.name} から退出しました。'
            await alert_channel.send(msg)

client.run("あなたのトークン")

このエントリを書いた理由

随分前に書かれたコードが最新版のdiscord.pyで動かないって困っている初心者の方がいたので、彼のためにその古いコードを最新版の discord.py (記事執筆時点でv1.1.1) 向けに書き直しました。
せっかくなので公開します。

12
9
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
12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?