forked from SamuelCurrid/Gompei-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdministration.py
More file actions
151 lines (116 loc) · 4.47 KB
/
Administration.py
File metadata and controls
151 lines (116 loc) · 4.47 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
143
144
145
146
147
148
149
150
151
import discord
import json
import os
from discord.ext import commands
from datetime import datetime
class Administration(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.logs = {}
self.load_state()
self.embed = discord.Embed(colour=discord.Colour.blue())
@commands.Cog.listener()
async def on_ready(self):
"""
Loads leaderboard states
"""
await self.update_guilds()
@commands.Cog.listener()
async def on_guild_join(self, guild):
"""
Creates default settings for new guilds
"""
self.logs["guildSettings"][str(guild.id)] = None
await self.update_state()
@commands.Cog.listener()
async def on_guild_remove(self, guild):
"""
Removes guild settings for logging
"""
self.logs["guildSettings"].pop(str(guild.id))
await self.update_state()
@commands.command(pass_context=True)
async def echo(self, ctx, arg1, *, arg2):
channel = ctx.guild.get_channel(int(arg1[2:-1]))
images = []
if len(ctx.message.attachments) > 0:
for i in ctx.message.attachments:
images.append(await i.to_file())
if channel is not None and ctx.message.author.guild_permissions.administrator or ctx.message.author.id == 87585011070414848:
await channel.send(arg2, files=images)
async def update_guilds(self):
savedGuilds = []
for guildID in self.logs["guildSettings"]:
savedGuilds.append(guildID)
guilds = []
for guild in self.bot.guilds:
guilds.append(str(guild.id))
addGuilds = [x for x in guilds if x not in savedGuilds]
removeGuilds = [x for x in savedGuilds if x not in guilds]
# Add new guilds
for guildID in addGuilds:
self.logs["guildSettings"][str(guildID)] = None
# Remove disconnected guilds
for guildID in removeGuilds:
if guildID != "guildSettings":
self.logs["guildSettings"].pop(str(guildID))
await self.update_state()
@commands.Cog.listener()
async def on_member_update(self, before, after):
for activity in after.activities:
foundActivity = False
if type(activity) is discord.activity.CustomActivity:
foundActivity = True
# Check new status
status = ""
if activity.emoji is not None:
if activity.emoji.is_custom_emoji():
status += "<:" + str(activity.emoji.name) + ":" + str(activity.emoji.id) + "> "
else:
status += activity.emoji.name + " "
if activity.name is not None:
status += activity.name
# Compare status before and after member update
if str(after.id) not in self.logs:
self.logs[str(after.id)] = status
await self.send_status_log(after, "None", status)
await self.update_state()
elif self.logs[str(after.id)] != status:
await self.send_status_log(after, str(self.logs[str(after.id)]), status)
self.logs[str(after.id)] = status
await self.update_state()
if not foundActivity:
if str(after.id) not in self.logs:
self.logs[str(after.id)] = None
await self.update_state()
if self.logs[str(after.id)] is not None:
await self.send_status_log(after, str(self.logs[str(after.id)]), None)
self.logs[str(after.id)] = None
await self.update_state()
break
async def update_state(self):
with open(os.path.join("config", "logging.json"), "r+") as loggingFile:
loggingFile.truncate(0)
loggingFile.seek(0)
json.dump(self.logs, loggingFile, indent=4)
def load_state(self):
with open(os.path.join("config", "logging.json"), "r+") as loggingFile:
logs = loggingFile.read()
self.logs = json.loads(logs)
async def send_status_log(self, user, before, after):
if self.logs["guildSettings"][str(user.guild.id)] is not None:
channel = user.guild.get_channel(int(self.logs["guildSettings"][str(user.guild.id)]))
self.embed.clear_fields()
self.embed.set_author(name=user.name + "#" + user.discriminator, icon_url=user.avatar_url)
self.embed.add_field(name="**Custom status update**", value="\n**Before:** " + str(before) + "\n**After:** " + str(after))
self.embed.set_footer(text="ID: " + str(user.id) + " • ")
await channel.send(embed=self.embed)
@commands.command(pass_context=True, name="logging")
async def change_logging(self, ctx):
if ctx.message.author.guild_permissions.administrator:
for channel in ctx.message.channel_mentions:
if self.logs["guildSettings"][str(ctx.message.guild.id)] != str(channel.id):
self.logs["guildSettings"][str(ctx.message.guild.id)] = str(channel.id)
print("Updating guild " + str(ctx.message.guild.id) + " to use logging channel " + str(channel.id))
await self.update_state()
print("Finished updating logging channel")