-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
142 lines (134 loc) · 4.6 KB
/
bot.py
File metadata and controls
142 lines (134 loc) · 4.6 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python3
''' Discord bot for Tur. '''
import os
import sys
import json
import discord
import logging as log
from lazylog import Logger
from discord.ext import commands as dcomm
current_dir = os.path.dirname(os.path.abspath(__file__))
term_specs = {"level": log.INFO, "splitLines": True, "pretty": True }
Logger.init(current_dir, termSpecs=term_specs)
log.getLogger("discord").setLevel(log.WARNING)
config = {}
prefix = '-'
with open(f'{current_dir}/config.json', encoding='utf-8') as cfgfh:
config = json.loads(cfgfh.read())
if 'token' not in config:
log.critical('Config is missing token')
sys.exit(1)
token = config['token']
if 'prefix' in config:
prefix = config['prefix']
intents = discord.Intents.default()
intents.members = True
dcbot = dcomm.Bot(command_prefix=prefix, intents=intents)
async def manage_voice_text_channels(member, guild, channel):
''' Create associated text channels and manage user access. '''
await guild.fetch_channels()
textchannel = None
textname = channel.name.lower().replace(' ','-')
members = 0
for cmember in channel.members:
if cmember.bot is True:
continue
members += 1
for chan in await guild.fetch_channels():
if chan.category != channel.category or chan.name.lower() != textname or str(chan.type).lower() != 'text':
continue
textchannel = chan
print(textchannel)
if textchannel is not None and members < 1:
#await textchannel.delete(
# reason='VC {textname} not in use'
#)
return True
if textchannel is None:
if members < 1:
return
musicbot = None
mbcmd = None
perms = discord.PermissionOverwrite(
read_messages=True,
send_messages=True,
create_instant_invite=False
)
# if '1' in textname:
# musicbot = await guild.fetch_member(836610895395946576)
# mbcmd = '!t1'
# if '2' in textname:
# musicbot = await guild.fetch_member(876922094510833664)
# mbcmd = '!t2'
overwrites = {
guild.default_role: discord.PermissionOverwrite(
read_messages=False,
send_messages=False,
view_channel=False,
create_instant_invite=False
),
guild.me: perms
}
if musicbot is not None:
overwrites[musicbot] = perms
for cmember in channel.members:
overwrites[cmember] = perms
#textchannel = await guild.create_text_channel(
# f'{textname}',
# overwrites=overwrites,
# category=channel.category,
# position=channel.position + 1,
# reason=f'VC {textname} in use'
#)
if mbcmd is not None:
await textchannel.send(
content=f'To use the music bot, commands start with {mbcmd}'
)
#if member not in channel.members:
#await textchannel.set_permissions(
# member,
# view_channel=False,
# read_messages=False,
# send_messages=False,
# create_instant_invite=False
# )
#else:
#await textchannel.set_permissions(
# member,
# view_channel=True,
# read_messages=True,
# send_messages=True,
# create_instant_invite=False
# )
if members < 1:
#await textchannel.delete(
# reason='VC {textname} not in use'
#)
print("%s is empty, removing" % (textname))
return True
return True
@dcbot.event
async def on_ready():
''' Once the bot is ready, log ready. '''
log.info("%s has connected to Discord!", dcbot.user)
await dcbot.change_presence(activity=discord.Game(name=config['activity'],type=1))
@dcbot.event
async def on_message(message):
''' Handle all messages except ones from the bot. '''
if message.author == dcbot.user:
return
await dcbot.process_commands(message)
return
'python' '/app/bot.py'
@dcbot.event
async def on_voice_state_update(member, before, after):
''' When a user changes voice state, check channels for neccessary action. '''
if before.channel is not None:
log.info("%s updated to %s", member, before.channel.name)
await manage_voice_text_channels(member, before.channel.guild, before.channel)
if after.channel is not None:
log.info("%s updated from to %s", member, after.channel.name)
await manage_voice_text_channels(member, after.channel.guild, after.channel)
return
if __name__ == '__main__':
dcbot.run(token)