-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
161 lines (149 loc) · 6.1 KB
/
bot.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
from logging42 import logger
import nextcord
from nextcord.ext import commands
import yaml
with open("config.yml", "r") as ymlfile:
cfg = yaml.full_load(ymlfile)
bot = commands.Bot()
# Load things from cfg
bot_token = cfg['bot token']
message = cfg['message']
# Check if moderation is enabled
def mod_enabled(guild_id):
enabled = False
with open("config.yml", "r") as ymlfile:
config = yaml.full_load(ymlfile)
try:
enabled = config['moderation'][guild_id]['enabled']
except BaseException as ex:
logger.warning(f'Tried to check if moderation is enabled, but failed! Error code: {ex}')
return enabled
def set_pref(guild, member, set: bool):
try:
with open("preferences.yml", "r") as file:
pref = yaml.full_load(file)
except FileNotFoundError:
pref = {}
if pref == None:
pref = {}
pref['warning'] = "DO NOT EDIT THIS FILE! It contains important user preferences."
if str(guild.id) in pref:
pref[str(guild.id)][str(member.id)] = set
else:
pref[str(guild.id)] = {}
pref[str(guild.id)][str(member.id)] = set
with open("preferences.yml", "w") as file:
yaml.dump(pref, file)
return
def check_pref(guild, member):
try:
with open("preferences.yml", "r") as file:
pref = yaml.full_load(file)
except FileNotFoundError:
return True
try:
ans = pref[str(guild.id)][str(member.id)]
except KeyError:
return True
return ans
## These functions load values live from yaml
# Check what channel to put mod logs in
def mod_channel(guild_id):
channel = 0
with open("config.yml", "r") as ymlfile:
config = yaml.full_load(ymlfile)
try:
channel = config['moderation'][guild_id]['channel']
except BaseException as ex:
logger.warning(f'Tried to check where to send logs, but failed! Error code: {ex}')
return channel
# Print to log when successfully logged in
@bot.event
async def on_ready():
logger.info(f'Logged in as {bot.user}')
if cfg['guilds status']['enabled']:
await bot.change_presence(activity=nextcord.Activity(type=nextcord.ActivityType.listening, name = cfg['guilds status']['status'].replace('[[number]]', str(len(bot.guilds)))))
logger.info(f'Status update: I\'m in {str(len(bot.guilds))} servers!')
# Add yaml entry on guild join
@bot.event
async def on_guild_join(guild):
logger.info(f'Joined a new guild! Name: {guild.name}, ID: {guild.id}')
with open("config.yml", "r") as ymlfile:
config = yaml.full_load(ymlfile)
config['moderation'][guild.id] = dict(enabled=False)
config['moderation'][guild.id] = dict(channel=0)
with open("config.yml", "w") as ymlfile:
yaml.dump(config, ymlfile)
logger.info(f'Added yml entry for guild {guild.name} (ID: {guild.id})')
if cfg['guilds status']['enabled']:
await bot.change_presence(activity=nextcord.Activity(type=nextcord.ActivityType.listening, name = cfg['guilds status']['status'].replace('[[number]]', str(len(bot.guilds)))))
logger.info(f'Status update: I\'m in {str(len(bot.guilds))} servers!')
# Command
@bot.slash_command(description='Say "F*ck you!" to someone (anonymously, except mods may have logging enabled)')
async def fu(interaction: nextcord.Interaction, user: nextcord.Member):
if interaction.guild == None:
await interaction.send("No.")
return
if not check_pref(interaction.guild, interaction.user):
await interaction.send("You cannot say that to this user!", ephemeral=True)
logger.success('NOT saying f*ck you to someone due to their preferences')
return
logger.debug(f'Saying f*ck you to {user.name} ({user.id}).')
if mod_enabled(interaction.guild_id):
log_chan = bot.get_channel(mod_channel(interaction.guild_id))
try:
await log_chan.send(f'User {interaction.user.name} (ID: {interaction.user.id}) used the `/fu` command on {user.name} (ID: {user.id})\
\nIn channel {interaction.channel.mention} (ID: {interaction.channel_id}).')
append = f'\n||*These commands are logged in this server.*||'
except nextcord.errors.Forbidden or nextcord.errors.ApplicationInvokeError:
append = f'\n||*Logging is enabled here, but failed.*||'
await interaction.channel.send(f'**⚠️ Logging Failed!**')
else:
append = f'\n||*These commands are **not** logged in this server.*||'
await interaction.send(f'Saying "{message.replace("[[mention]]", user.mention)}"...{append}', ephemeral=True)
if user.id == bot.user.id:
send_message = message.replace('[[mention]]', interaction.user.mention)
else:
send_message = message.replace('[[mention]]', user.mention)
await interaction.channel.send(send_message)
# Mod config
@bot.slash_command(description='Whether to log uses of `/fu`, and where to put the log')
async def mod(interaction: nextcord.Interaction, enabled: bool, channel: nextcord.TextChannel):
if interaction.guild == None:
await interaction.send("No.")
return
if interaction.user.guild_permissions.administrator:
if enabled:
with open("config.yml", "r") as ymlfile:
config = yaml.full_load(ymlfile)
config['moderation'][interaction.guild_id]['enabled'] = True
config['moderation'][interaction.guild_id]['channel'] = channel.id
with open("config.yml", "w") as ymlfile:
yaml.dump(config, ymlfile)
await interaction.send(f'Moderation logs are now **Enabled**, in {channel.mention}!')
else:
with open("config.yml", "r") as ymlfile:
config = yaml.full_load(ymlfile)
config['moderation'][interaction.guild_id]['enabled'] = False
config['moderation'][interaction.guild_id]['channel'] = 0
with open("config.yml", "w") as ymlfile:
yaml.dump(config, ymlfile)
await interaction.send(f'Moderation logs are now **Disabled**!')
else:
await interaction.send(f'You don\'t have permission!')
# No fu moment
@bot.slash_command(description="Make yourself un-fu-able")
async def nofu(interaction: nextcord.Interaction, fu: str = nextcord.SlashOption(required=True, description="Do you want people to use fu on you?", choices=["Yes","No"])):
if interaction.guild == None:
await interaction.send("No.")
return
if fu == "Yes":
set_pref(interaction.guild, interaction.user, True)
desc = "can"
else:
set_pref(interaction.guild, interaction.user, False)
desc = "cannot"
await interaction.send(f"You now **{desc}** be `/fu`ed!")
logger.info("Updated preferences for a user.")
# Run the Bot
bot.run(bot_token)