repl.it
@TheDrone7/

riv-man

Python

Repl.it discord voice-channel manager

fork
loading
Files
  • main.py
  • bg_tasks.py
  • database.py
  • keep_alive.py
  • utils.py
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import sys, os
sys.exit()
with open('.env', 'r') as env:
        for line in env.read().split('\n'):
                if '=' in line:
                        line_split = line.split('=')
                        os.environ[line_split[0]] = line_split[1]
import utils
import discord
from database import data
from datetime import datetime
from bg_tasks import bg_tasks
from discord.ext import commands
from keep_alive import keep_alive
from utils import log, find_date, t_open, t_close

bot = commands.Bot(command_prefix='v!')

allowed_roles = [
	437061464990285824,
	481953084726312977,
	438855279363489812,
	503653160955674636
]   

channel_id = 624726478432501770

open_aliases = [
	'o',
	'openchannel',
	'openvoice',
	'ov',
	'oc'
]

close_aliases = [
	'c',
	'closechannel',
	'closevoice',
	'cv',
	'cc'
]

mute_aliases = [
	'm',
	'hush',
	'silence',
	'quiet'
]


def check_roles(ctx):
	author = ctx.author
	good = False

	for role in author.roles:
		if role.id in allowed_roles:
			good = True
	
	return good


@commands.check(check_roles)
@bot.command(name='mute', aliases=mute_aliases)
async def mute(
	ctx,
	user: discord.Member,
	time: str,
	reason: str
):
	author = ctx.author
	for r in user.roles:
		if r in allowed_roles:
			await ctx.send("You can't mute annother helper!")
	await user.edit(mute=True)
	fields = [('**Reason:**', reason, True)]
	try:
		find_date(time)
		mutes = data.get_mutes()
		time = find_date(time)
		mutes[user.id] = time
		await data.set_mutes(mutes)
		duration = time - datetime.now()
		duration = duration.strftime("%Y-%m-%w-%W %H:%M:%S")
		fields.append(('**Duration:**', duration, True))
	except utils.InvalidDate:
		pass

	await log(
		'**Mute**',
		f'<@{author.id}> muted <@{user.id}>.',
		0xff0000,
		ctx=ctx,
		fields=fields
	)


@bot.event
async def on_ready():
	print(f'Connected as {bot.user}')
	if not data.initialized:
		await data.set_mutes({'None': 'None'})
		await data.init()
		await bg_tasks(bot)


@commands.check(check_roles)
@bot.command(name='open', aliases=open_aliases)	# Just in case it fails to do so automatically
async def open_channel(ctx):
	guild = ctx.guild
	everyone = guild.default_role
	channel = guild.get_channel(624726478432501770)
	await t_open(guild)
	await channel.set_permissions(everyone, connect=True, speak=True)
	author = ctx.author
	await log(
		'**Channel Opened**',
		f'<@{author.id}> Opened the voice channel.',
		0x019406,
		guild=guild,
		user=author
	)


@commands.check(check_roles)
@bot.command(name='close', aliases=close_aliases)
async def close_channel(ctx):
	guild = ctx.guild
	channel = guild.get_channel(624726478432501770)
	everyone = guild.default_role
	await t_close(guild, allowed_roles)
	
	await channel.set_permissions(everyone, connect=False, speak=False)
	author = ctx.author
	await log(
		'**Channel Closed**',
		f'<@{author.id}> Closed the voice channel.',
		0x661710,
		guild=guild,
		user=author
	)


class ChannelUserList:
	users = []

	def handle_user(self, user):
		if user.voice is None:
			if user.id in self.users:
				self.users.remove(user.id)
				return True
			return False

		if user.voice.channel.id != channel_id:
			if user.id in self.users:
				self.users.remove(user.id)
				return True
			return False
		if user.voice.channel.id == channel_id:
			print('ya')
			self.users.append(user.id)
			return True
		print('wat')
		return False


user_list = ChannelUserList()


@bot.event
async def on_voice_state_update(member, state_before, state_after):
	guild = member.guild
	channel = guild.get_channel(channel_id)
	o = False	# Whether the voice channel is to be opened.
	everyone = guild.default_role	 # The role representing everyone
	print(type(everyone))
	user = None

	if not user_list.handle_user(member):
		return
	print('past dat')
	for m in channel.members:
		for role in m.roles:
			if role.id in allowed_roles:
				user = m
				o = True

	if o:
		normal_user = guild.get_member(527937324865290260)
		if not channel.permissions_for(normal_user).speak:
			await t_open(guild)
			await channel.set_permissions(everyone, connect=True, speak=True)
			await log(
				'**Channel Opened**',
				'Channel was automatically opened because a helper joined the channel.',
				0x019406,
				guild=guild,
				user=user
			)

	else:
		for r in member.roles:

			if r.id in allowed_roles:
				await log(
				'**Channel Closed**',
				'Channel was automatically closed because the last helper left the channel.',
				0x661710,
				guild=guild,
				user=member
			)
		await t_close(guild, allowed_roles)
		await channel.set_permissions(everyone, connect=False, speak=False)
		for role_id in allowed_roles:
			role = guild.get_role(role_id)
			await channel.set_permissions(role, connect=True, speak=True)



bot.run(os.getenv('TOKEN'))
?