Skip to content

Commit

Permalink
Log servers on start, cleanup server leave code
Browse files Browse the repository at this point in the history
  • Loading branch information
ankith26 committed Aug 30, 2024
1 parent 2ec4202 commit f82a05b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
4 changes: 3 additions & 1 deletion bot/config_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@


class ConfigEntry(TypedDict):
configname: str
grantroles: set[str]
deleteroles: set[str]
is_academic: bool
Expand Down Expand Up @@ -38,6 +39,7 @@ def read_and_validate_config(config_file_path: str):
section_obj = server_config[section]
try:
cur = ret[section_obj.getint("serverid")] = {
"configname": section,
"grantroles": set(
i for i in section_obj.get("grantroles").split(",") if i
),
Expand All @@ -51,7 +53,7 @@ def read_and_validate_config(config_file_path: str):
print(f"ERROR: Invalid section '{section}'!")
return None

if len(section_obj.keys()) != (len(cur) + 1):
if len(section_obj.keys()) != len(cur):
print(f"ERROR: Got invalid amount of keys in {section}")
return None

Expand Down
42 changes: 29 additions & 13 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ async def set_nickname(member: discord.Member, server_config: ConfigEntry):
await member.edit(nick=realname)


async def leave_guild(guild: discord.Guild, channel: discord.abc.Messageable | None):
"""
Send a message to the given channel and leave the given guild.
"""
if channel:
await channel.send(
"This server is not authorized to work with CAS-bot. "
"Read the instructions to invite the bot in the project README",
)
await guild.leave()


async def post_verification(
ctx: commands.Context, member: discord.Member | discord.User
):
Expand All @@ -222,11 +234,7 @@ async def post_verification(

server_config = server_configs.get(ctx.guild.id)
if server_config is None:
await ctx.reply(
"This server is not authorized to work with CAS-bot. Read the instructions to invite the bot in the project README",
ephemeral=True,
)
await ctx.guild.leave()
await leave_guild(ctx.guild, ctx.channel)
return

try:
Expand Down Expand Up @@ -420,24 +428,22 @@ async def roll_or_query_error(ctx: commands.Context, error: Exception):
@bot.event
async def on_guild_join(guild: discord.Guild):
server_config = server_configs.get(guild.id)

welcome_message = "CAS-bot has joined this server"
first_channel = get_first_channel_with_permission(guild)

if not server_config:
welcome_message = "This server is not authorized to work with CAS-bot. Read the instructions to invite the bot in the project README."
if first_channel:
await first_channel.send(welcome_message)
await guild.leave()
await leave_guild(guild, first_channel)
return

if first_channel:
await first_channel.send(welcome_message)
await first_channel.send("CAS-bot has joined this server")


@bot.event
async def on_ready():
"""This is executed when the bot connects to a server."""
if not bot.user:
print(f"Failed to connect to discord! bot.user not defined")
return

print(f"{bot.user.name} has connected to Discord!")
try:
synced = await bot.tree.sync()
Expand All @@ -450,6 +456,16 @@ async def on_ready():

bot.loop.create_task(webserver())

print(f"The bot is a member in {len(bot.guilds)} guild(s).")
for guild in bot.guilds:
server_config = server_configs.get(guild.id)
if not server_config:
print(f" - LEAVING: {guild.id} [{guild.name}]")
await leave_guild(guild, get_first_channel_with_permission(guild))
continue

print(f" - {guild.id}: {guild.name} [{server_config['configname']}]")


if __name__ == "__main__":
bot.run(TOKEN)

0 comments on commit f82a05b

Please sign in to comment.