Skip to content

Instantly share code, notes, and snippets.

@leoherzog
Last active May 9, 2024 15:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoherzog/99d6eafaf549175f4115473ccd219f2c to your computer and use it in GitHub Desktop.
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
#!/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