-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #384 from PyBotDevs/add-autorole-serverconfig-cmd
Add `/serverconfig autorole` to allow server managers to automatically assign roles to newly-joined members
- Loading branch information
Showing
3 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Imports | ||
import discord | ||
from discord import option, ApplicationContext | ||
from discord.commands import SlashCommandGroup | ||
from discord.ext import commands | ||
from framework.isobot.db import serverconfig | ||
|
||
# Variables | ||
serverconf = serverconfig.ServerConfig() | ||
|
||
# Functions | ||
class ServerConfig(commands.Cog): | ||
def __init__(self, bot): | ||
self.bot = bot | ||
|
||
serverconfig_cmds = SlashCommandGroup(name="serverconfig", description="Commands related to server customization and configuration.") | ||
|
||
@serverconfig_cmds.command( | ||
name="autorole", | ||
description="Set a role to provide to all newly-joined members of the server." | ||
) | ||
@option(name="role", description="The role that you want to automatically give to all new members.", type=discord.Role, default=None) | ||
async def autorole(self, ctx: ApplicationContext, role: discord.Role = None): | ||
"""Set a role to provide to all newly-joined members of the server.""" | ||
if not ctx.author.guild_permissions.manage_guild: | ||
return await ctx.respond("You can't use this command! You need the `Manage Server` permission to run this.", ephemeral=True) | ||
if role != None: | ||
serverconf.set_autorole(ctx.guild.id, role.id) | ||
localembed = discord.Embed( | ||
title=f":white_check_mark: Autorole successfully set for **{ctx.guild.name}**!", | ||
description=f"From now onwards, all new members will receive the {role.mention} role.", | ||
color=discord.Color.green() | ||
) | ||
else: | ||
serverconf.set_autorole(ctx.guild.id, None) | ||
localembed = discord.Embed( | ||
title=f":white_check_mark: Autorole successfully disabled for **{ctx.guild.name}**", | ||
description="New members will not automatically receive any roles anymore.", | ||
color=discord.Color.green() | ||
) | ||
await ctx.respond(embed=localembed) | ||
|
||
def setup(bot): | ||
bot.add_cog(ServerConfig(bot)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""The framework module library used for managing server setup configurations.""" | ||
|
||
# Imports | ||
import json | ||
from framework.isobot.colors import Colors as colors | ||
|
||
# Functions | ||
class ServerConfig: | ||
def __init__(self): | ||
print(f"[framework/db/Automod] {colors.green}ServerConfig db library initialized.{colors.end}") | ||
|
||
def load(self) -> dict: | ||
"""Fetches and returns the latest data from the items database.""" | ||
with open("database/serverconfig.json", 'r', encoding="utf8") as f: db = json.load(f) | ||
return db | ||
|
||
def save(self, data: dict) -> int: | ||
"""Dumps all cached data to your local machine.""" | ||
with open("database/serverconfig.json", 'w+', encoding="utf8") as f: json.dump(data, f) | ||
return 0 | ||
|
||
def generate(self, server_id: int) -> int: | ||
"""Generates a new database key for the specified server/guild id in the automod database.""" | ||
serverconf = self.load() | ||
if str(server_id) not in serverconf: | ||
serverconf[str(server_id)] = { | ||
"autorole": None, | ||
"welcome_message": { | ||
"channel": None, | ||
"message": None | ||
}, | ||
"goodbye_message": { | ||
"channel": None, | ||
"message": None | ||
} | ||
} | ||
self.save(serverconf) | ||
return 0 | ||
|
||
def fetch_raw(self, server_id: int) -> dict: | ||
"""Fetches the current server configuration data for the specified guild id, and returns it as a `dict`.""" | ||
serverconf = self.load() | ||
return serverconf[str(server_id)] | ||
|
||
def fetch_autorole(self, server_id: int) -> str: | ||
"""Fetch the specified autorole for the server. Returns `None` if not set.""" | ||
return self.fetch_raw(server_id)["autorole"] | ||
|
||
def fetch_welcome_message(self, server_id: int) -> dict: | ||
"""Fetches the welcome message and set channel for the server as `dict`.\n\nReturns `None` for `channel` and `message` values if not set.""" | ||
return self.fetch_raw(server_id)["welcome_message"] | ||
|
||
def fetch_goodbye_message(self, server_id: int) -> dict: | ||
"""Fetches the goodbye message and set channel for the server as `dict`.\n\nReturns `None` for `channel` and `message` values if not set.""" | ||
return self.fetch_raw(server_id)["goodbye_message"] | ||
|
||
def set_autorole(self, server_id: int, role_id: int) -> int: | ||
"""Sets a role id to use as autorole for the specified guild. Returns `0` if successful.""" | ||
serverconf = self.load() | ||
serverconf[str(server_id)]["autorole"] = role_id | ||
self.save(serverconf) | ||
|
||
def set_welcome_message(self, server_id: int, channel_id: int, message: str) -> int: | ||
"""Sets a channel id to send a custom welcome message to, for the specified guild. Returns `0` if successful.""" | ||
serverconf = self.load() | ||
serverconf[str(server_id)]["welcome_message"]["channel"] = channel_id | ||
serverconf[str(server_id)]["welcome_message"]["message"] = message | ||
self.save(serverconf) | ||
|
||
def set_goodbye_message(self, server_id: int, channel_id: int, message: str) -> int: | ||
"""Sets a channel id to send a custom goodbye message to, for the specified guild. Returns `0` if successful.""" | ||
serverconf = self.load() | ||
serverconf[str(server_id)]["goodbye_message"]["channel"] = channel_id | ||
serverconf[str(server_id)]["goodbye_message"]["message"] = message | ||
self.save(serverconf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters