Last active
May 9, 2024 15:31
-
-
Save leoherzog/99d6eafaf549175f4115473ccd219f2c to your computer and use it in GitHub Desktop.
Python Discord Bot to post to a text channel when people join or leave a voice channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import discord | |
import random | |
intents = discord.Intents.default() | |
intents.voice_states = True | |
client = discord.Client(intents=intents) | |
join_phrases = ['π **{0}** joined **{1}**', 'π Hi **{0}**! Welcome to **{1}**', 'π Welcome to **{1}**, **{0}**!', 'π€© EVERYONE SHUT UP! **{0}** is in **{1}** now.'] | |
leave_phrases = ['π **{0}** left **{1}**', 'π We miss you already, **{0}**! You left **{1}**', 'ππ Oh no! **{1}** has lost a member. Goodbye, **{0}**', 'βοΈ Goodbye, **{0}**! You left **{1}**'] | |
move_phrases = ['π **{0}** moved from **{1}** to **{2}**', 'π **{0}** just went from **{1}** to **{2}**', 'βͺοΈ **{0}** hopped from **{1}** to **{2}**', 'βοΈ Where did **{0}** go? Oh. Right. They were in **{1}**, but now they\'re in **{2}**.', 'π **{0}** is on the move from **{1}** to **{2}**!'] | |
@client.event | |
async def on_voice_state_update(member, before, after): | |
if before.channel == after.channel: | |
# no channel change. must be something like a mute/deafen. | |
# do nothing with this event | |
return | |
channel = discord.utils.get(client.get_all_channels(), name="voice-notifications") # destination channel name | |
if before.channel is None and after.channel is not None: | |
print(member.nick or member.name + ' joined ' + after.channel.name) | |
await channel.send(random.choice(join_phrases).format(member.mention, after.channel.mention)) | |
elif before.channel is not None and after.channel is None: | |
print(member.nick or member.name + ' left ' + before.channel.name) | |
await channel.send(random.choice(leave_phrases).format(member.mention, before.channel.mention)) | |
elif before.channel != after.channel: | |
print(member.nick or member.name + ' moved from ' + before.channel.name + ' to ' + after.channel.name) | |
await channel.send(random.choice(move_phrases).format(member.mention, before.channel.mention, after.channel.mention)) | |
client.run("BOT_TOKEN") # replace with your bot token |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment