Skip to content

Commit

Permalink
Moved changes from old develop format to the new one (#151)
Browse files Browse the repository at this point in the history
* Moved changes from old develop format to the new one

* Resolved conflicts
  • Loading branch information
Ryth-cs authored Aug 29, 2021
1 parent 51039ac commit 3316c40
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 139 deletions.
139 changes: 64 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,81 +67,70 @@ $ source secrets.env
```bash
$ cd src
```

5. Install all the requirements for python:
```bash
pip install -r requirements.txt
```

6. Run the bot:
```bash
python3 main.py
```

## Current Functions

The list below describes the different "Cogs" of the bot, their associated commands, and any additional information required to set them up.

<details>
<summary>Voicemaster</summary>

### Voicemaster

#### !setvmmaster <channel_id>

* Make the given ID a Voicemaster master.

#### !getvmmasters * Get all the Voicemaster masters in the server.

#### !removevmmaster <channel_id>

* Remove the given ID as a Voicemaster master.

#### !removeallmasters * Remove all Voicemaster masters from the server.

#### !killallslaves * Kill all the Voicemaster slave channels in the server.

#### !lockvm * Locks the Voicemaster slave you're currently in to the number of current members.

#### !unlockvm * Unlocks the Voicemaster slave you're currently in.
</details>

<details>
<summary>Default Role</summary>

### Default role

#### !setdefaultrole <role_mention | role_id> * Set the default role to the @'ed role or given role ID.

#### !getdefaultrole * Gets the current default role value.

#### !removedefaultrole * Removes the current default role.
</details>

<details>
<summary>Log Channel</summary>

### Log Channel

#### !setlogchannel <channel_mention | channel_id> * Set the log channel to the #'ed channel or given role ID.

#### !getlogchannel * Gets the current log channel value.

#### !removelogchannel * Removes the current log channel value.

</details>

<details>
<summary>Administrator Tools</summary>

### Administrator Tools

Adds a few commands useful for admin operations.

#### !clear_message * Aliases: `cls, purge, delete`
* Clear the specified number of messages from the current text channel.

#### !members
5. Install all the requirements for python:
```bash
pip install -r requirements.txt
```
6. Run the bot:
```bash
python3 main.py
```

## Current Functions
The list below describes the different "Cogs" of the bot, their associated commands, and any additional information required to set them up.

<details>
<summary>Voicemaster</summary>

### Voicemaster
#### !setvmmaster <channel_id>
* Make the given ID a Voicemaster master.

#### !getvmmasters * Get all the Voicemaster masters in the server.

#### !removevmmaster <channel_id>
* Remove the given ID as a Voicemaster master.

#### !removeallmasters * Remove all Voicemaster masters from the server.

#### !killallslaves * Kill all the Voicemaster slave channels in the server.

#### !lockvm * Locks the Voicemaster slave you're currently in to the number of current members.

#### !unlockvm * Unlocks the Voicemaster slave you're currently in.
</details>

<details>
<summary>Default Role</summary>

### Default role
#### !setdefaultroles <role_mention | role_id> * Sets the roles that the server gives to members when they join the server.

#### !getdefaultroles * Gets the current default roles set for the server.

#### !removedefaultroles * Removes the current default roles for the server.
</details>

<details>
<summary>Log Channel</summary>

### Log Channel
#### !setlogchannel <channel_mention | channel_id> * Set the log channel to the #'ed channel or given role ID.

#### !getlogchannel * Gets the current log channel value.

#### !removelogchannel * Removes the current log channel value.
</details>

<details>
<summary>Administrator Tools</summary>

### Administrator Tools
Adds a few commands useful for admin operations.
#### !clear_message * Aliases: `cls, purge, delete`
* Clear the specified number of messages from the current text channel.

#### !members
* List the current number of members in the server.

#### !remove-cog \<cog name>
Expand Down
134 changes: 77 additions & 57 deletions src/esportsbot/cogs/DefaultRoleCog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from discord.ext import commands
from esportsbot.base_functions import role_id_from_mention
from esportsbot.db_gateway import DBGatewayActions
from esportsbot.models import Guild_info
from esportsbot.models import Guild_info, Default_roles
from esportsbot.base_functions import role_id_from_mention, send_to_log_channel


class DefaultRoleCog(commands.Cog):
Expand All @@ -14,15 +15,24 @@ async def on_member_join(self, member):
guild = DBGatewayActions().get(Guild_info, guild_id=member.guild.id)
if not guild:
return

if guild.default_role_id:
default_role = member.guild.get_role(guild.default_role_id)
await member.add_roles(default_role)
# Get all the default role for the server from database
guild_default_roles = DBGatewayActions().list(Default_roles, guild_id=member.guild.id)
# Check to see if any roles exist
if guild_default_roles:
# Create list of roles from database response
apply_roles = [member.guild.get_role(role.role_id) for role in guild_default_roles]
# Add all the roles to the user, we don't check if they're valid as we do this on input
await member.add_roles(*apply_roles)
await self.bot.adminLog(
None,
{
"Cog": str(type(self)),
"Message": f"{member.mention} has joined the server and received the {default_role.mention} role"
"Cog":
str(type(self)),
"Message":
self.STRINGS['default_role_join'].format(
member_name=member.mention,
role_ids=(' '.join(f'<@&{x.id}>' for x in apply_roles))
)
},
guildID=member.guild.id
)
Expand All @@ -31,76 +41,86 @@ async def on_member_join(self, member):
None,
{
"Cog": str(type(self)),
"Message": f"{member.mention} has joined the server"
"Message": self.STRINGS['default_role_join_no_role'].format(member_name=member.mention)
},
guildID=member.guild.id
)

@commands.command(
name="setdefaultrole",
usage="<role_id> or <@role>",
help="Sets the role that the server gives to members when they join the server"
name="setdefaultroles",
usage="<@role> <@role> <@role> ...",
help="Sets the roles that the server gives to members when they join the server"
)
@commands.has_permissions(administrator=True)
async def setdefaultrole(self, ctx, given_role_id):
cleaned_role_id = role_id_from_mention(given_role_id) if given_role_id else False
if cleaned_role_id:
guild = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id)

if not guild:
db_item = Guild_info(guild_id=ctx.guild.id, default_role_id=cleaned_role_id)
DBGatewayActions().create(db_item)
else:
guild.default_role_id = cleaned_role_id
DBGatewayActions().update(guild)

await ctx.channel.send(self.STRINGS['default_role_set'].format(role_id=cleaned_role_id))
default_role = ctx.author.guild.get_role(cleaned_role_id)
await self.bot.adminLog(
ctx.message,
{
"Cog":
str(type(self)),
"Message":
self.STRINGS['default_role_set_log'].format(author=ctx.author.mention,
role_mention=default_role.mention)
}
)
async def setdefaultroles(self, ctx, *, args: str):
role_list = args.split(" ")
if len(role_list) == 0:
await ctx.channel.send(self.STRINGS['default_roles_set_empty'])
else:
await ctx.channel.send(self.STRINGS['default_role_set_missing_params'])
checked_roles = []
checking_error = False
# Loop through the roles to check the input formatting is correct and that roles exist
for role in role_list:
try:
# Clean the inputted role to just the id
cleaned_role_id = role_id_from_mention(role)
# Obtain role object from the guild to check it exists
role_obj = ctx.author.guild.get_role(cleaned_role_id)
# Add role to array to add post checks
checked_roles.append(cleaned_role_id)
except Exception as err:
print(err)
checking_error = True
if not checking_error:
for role in checked_roles:
DBGatewayActions().create(Default_roles(guild_id=ctx.author.guild.id, role_id=role))
await ctx.channel.send(self.STRINGS['default_roles_set'].format(roles=args))
await self.bot.adminLog(
ctx.message,
{
"Cog": str(type(self)),
"Message": self.STRINGS['default_roles_set_log'].format(author_mention=ctx.author.mention,
roles=args)
}
)
else:
await ctx.channel.send(self.STRINGS['default_roles_set_error'])

@commands.command(
name="getdefaultrole",
name="getdefaultroles",
usage="",
help="Gets the role that the server gives to members when they join the server"
help="Gets the roles that the server gives to members when they join the server"
)
@commands.has_permissions(administrator=True)
async def getdefaultrole(self, ctx):
guild = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id)
if not guild:
await ctx.channel.send(self.STRINGS['default_role_missing'])
return

if guild.default_role_id:
await ctx.channel.send(self.STRINGS['default_role_get'].format(role_id=guild.default_role_id))
async def getdefaultroles(self, ctx):
# Get all the default role for the server from database
guild_default_roles = DBGatewayActions().list(Default_roles, guild_id=ctx.author.guild.id)
# Check to see if any roles exist
if guild_default_roles:
# Create list of roles from database response
apply_roles = [ctx.author.guild.get_role(role.role_id) for role in guild_default_roles]
# Return all the default roles to the user
await ctx.channel.send(
self.STRINGS['default_role_get'].format(role_ids=(' '.join(f'<@&{x.id}>' for x in apply_roles)))
)
else:
await ctx.channel.send(self.STRINGS['default_role_missing'])

@commands.command(
name="removedefaultrole",
name="removedefaultroles",
usage="",
help="Removes the role that the server gives to members when they join the server"
help="Removes the roles that the server gives to members when they join the server"
)
@commands.has_permissions(administrator=True)
async def removedefaultrole(self, ctx):
guild = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id)
if not guild:
await ctx.channel.send(self.STRINGS['default_role_missing'])
return

if guild.default_role_id:
guild.default_role_id = None
DBGatewayActions().update(guild)
async def removedefaultroles(self, ctx):
# Get all the default role for the server from database
guild_default_roles = DBGatewayActions().list(Default_roles, guild_id=ctx.author.guild.id)
# Check to see if any roles exist
if guild_default_roles:
for default_role in guild_default_roles:
# Remove the current role
DBGatewayActions().delete(default_role)
# Return a response to the user
await ctx.channel.send(self.STRINGS['default_role_removed'])
await self.bot.adminLog(
ctx.message,
Expand Down
8 changes: 7 additions & 1 deletion src/esportsbot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ class Guild_info(base):
__tablename__ = 'guild_info'
guild_id = Column(BigInteger, primary_key=True, nullable=False)
log_channel_id = Column(BigInteger, nullable=True)
default_role_id = Column(BigInteger, nullable=True)


class Default_roles(base):
__tablename__ = 'default_roles'
default_roles_id = Column(BigInteger, primary_key=True, autoincrement=True, nullable=False)
guild_id = Column(BigInteger, nullable=False)
role_id = Column(BigInteger, nullable=False)


class Pingable_polls(base):
Expand Down
15 changes: 9 additions & 6 deletions src/esportsbot/user_strings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ members = "This server has {member_count} members including me"
no_version = "No version recorded"

[default_role]
default_role_missing = "Default role has not been set"
default_role_join = "{member_name} has joined the server and received: {role_ids}"
default_role_join_no_role = "{member_name} has joined the server"
default_role_missing = "Default role(s) have not been set"

default_role_set = "Default role has been set to {role_id}"
default_role_set_log = "{author} has set the default role to {role_mention}"
default_role_set_missing_param = "You need to either @ a role or paste the ID"
default_roles_set = "Default role(s) are now set to {roles}"
default_roles_set_empty = "No roles were passed, please review your usage"
default_roles_set_error = "Error occurred during this operation, please check that you have formatted these inputs correctly"
default_role_set_log = "{author_mention} has set the default role(s) to: {roles}"

default_role_get = "Default role is set to {role_id}"
default_role_get = "Default role(s) are set to {role_ids}"

default_role_removed = "Default role has been removed"
default_role_removed = "Default role(s) are removed"
default_role_removed_log = "{author_mention} has removed the default role"

[music]
Expand Down

0 comments on commit 3316c40

Please sign in to comment.