-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
122 lines (93 loc) · 3.15 KB
/
Copy pathmain.py
File metadata and controls
122 lines (93 loc) · 3.15 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import discord
import os
import random
import asyncio
intents = discord.Intents.default()
intents.members = True
intents.presences = True
client = discord.Client(intents=intents)
#global state and target variables
state = 0
target = None
@client.event
async def on_voice_state_update(member, before, after):
"""
Main function, moves everyone but the target and sets a new one if state == 1.
Called when a member changes their VoiceState.
Parameters
member (Member): Member whose voice states changed
before (VoiceState): Voice State prior to the changes
after (VoiceState): Voice state after the changes
"""
if member.id == target.id:
if after.channel is not None:
await asyncio.sleep(1)
await move_members(member, after.channel)
if state == 1:
set_target(get_random_victim(member))
async def move_members(member, current_channel):
"""
Function used to move members one voice channel up
Parameters
member (Member): member who is to be moved
current_channel (VoiceChannel): channel in which the member is located
"""
this_index = member.guild.voice_channels.index(current_channel)
vc_len = len((member.guild.voice_channels))
if (this_index < (vc_len - 1)):
target_channel = member.guild.voice_channels[this_index + 1]
else:
target_channel = member.guild.voice_channels[0]
for member in member.voice.channel.members:
if member != target:
try:
await member.move_to(target_channel)
except:
pass
def get_random_victim(member):
"""
Function that chooses a new random victim from the member's voice channel
Parameters:
member (Member): target who is to be replaced
"""
target_channel = member.voice.channel
random_target = random.choice(target_channel.members)
return random_target
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
"""
Function that reacts to messages and sets the state and target (if state is 1)
"""
#ignore messages from the bot itself
if message.author == client.user:
return
#sets state to random (0)
if message.content.startswith('$random'):
set_target(get_random_victim(message.author))
state_to_1()
await move_members(message.author, message.author.voice.channel)
#sets state to choose (1)
if message.content.startswith('$choose') and len(message.mentions) == 1:
set_target(message.mentions[0])
state_to_2()
await move_members(message.author, message.author.voice.channel)
#tool to check the state in the console
if message.content.startswith('$state'):
print('current state: ', state)
print('current target: ', target)
def state_to_1():
global state
state = 1
return state
def state_to_2():
global state
state = 2
return state
def set_target(new_target):
global target
target = new_target
return target
client.run(os.getenv('TOKEN'))