-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (55 loc) · 2.37 KB
/
main.py
File metadata and controls
67 lines (55 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import discord
from discord.ext import commands
import asyncio
TOKEN = "" # Replace with your bot token
SOUND_FILE = "1380208997826695320.ogg" # or .wav
LAST_SOUND_FILE = "1342180479289266186.ogg"
TARGET_USER_ID = 0 # <-- Only this user triggers the bot
intents = discord.Intents.default()
intents.voice_states = True # needed for voice updates
intents.guilds = True
intents.members = True # make sure "Server Members Intent" is enabled in Dev Portal
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
@bot.event
async def on_voice_state_update(member, before, after):
# Only react for the target user (and ignore bots)
if member.bot or member.id != TARGET_USER_ID:
return
# Trigger only when they actually join or move to a new channel
if after.channel is not None and before.channel != after.channel:
channel = after.channel
print(f"{member} joined {channel.name}")
# Wait 1.5 seconds before playing
await asyncio.sleep(1.5)
vc = member.guild.voice_client
try:
if vc and vc.is_connected():
if vc.channel.id != channel.id:
await vc.move_to(channel)
else:
vc = await channel.connect()
# Play the sound for each user in the channel (excluding bots)
for user in channel.members:
if user.bot:
continue
# Play sound at 50% volume
audio = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(SOUND_FILE), volume=0.5)
if vc.is_connected():
vc.play(audio)
while vc.is_playing():
await asyncio.sleep(0.5)
# Play last sound if still connected
if vc.is_connected():
audio = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(LAST_SOUND_FILE), volume=0.5)
vc.play(audio)
while vc.is_playing():
await asyncio.sleep(0.5)
except discord.errors.ClientException as e:
print(f"Voice client error: {e}")
finally:
if member.guild.voice_client and member.guild.voice_client.is_connected():
await member.guild.voice_client.disconnect()
bot.run(TOKEN)