Skip to content

Commit 3316c40

Browse files
authored
Moved changes from old develop format to the new one (#151)
* Moved changes from old develop format to the new one * Resolved conflicts
1 parent 51039ac commit 3316c40

File tree

4 files changed

+157
-139
lines changed

4 files changed

+157
-139
lines changed

README.md

Lines changed: 64 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -67,81 +67,70 @@ $ source secrets.env
6767
```bash
6868
$ cd src
6969
```
70-
71-
5. Install all the requirements for python:
72-
```bash
73-
pip install -r requirements.txt
74-
```
75-
76-
6. Run the bot:
77-
```bash
78-
python3 main.py
79-
```
80-
81-
## Current Functions
82-
83-
The list below describes the different "Cogs" of the bot, their associated commands, and any additional information required to set them up.
84-
85-
<details>
86-
<summary>Voicemaster</summary>
87-
88-
### Voicemaster
89-
90-
#### !setvmmaster <channel_id>
91-
92-
* Make the given ID a Voicemaster master.
93-
94-
#### !getvmmasters * Get all the Voicemaster masters in the server.
95-
96-
#### !removevmmaster <channel_id>
97-
98-
* Remove the given ID as a Voicemaster master.
99-
100-
#### !removeallmasters * Remove all Voicemaster masters from the server.
101-
102-
#### !killallslaves * Kill all the Voicemaster slave channels in the server.
103-
104-
#### !lockvm * Locks the Voicemaster slave you're currently in to the number of current members.
105-
106-
#### !unlockvm * Unlocks the Voicemaster slave you're currently in.
107-
</details>
108-
109-
<details>
110-
<summary>Default Role</summary>
111-
112-
### Default role
113-
114-
#### !setdefaultrole <role_mention | role_id> * Set the default role to the @'ed role or given role ID.
115-
116-
#### !getdefaultrole * Gets the current default role value.
117-
118-
#### !removedefaultrole * Removes the current default role.
119-
</details>
120-
121-
<details>
122-
<summary>Log Channel</summary>
123-
124-
### Log Channel
125-
126-
#### !setlogchannel <channel_mention | channel_id> * Set the log channel to the #'ed channel or given role ID.
127-
128-
#### !getlogchannel * Gets the current log channel value.
129-
130-
#### !removelogchannel * Removes the current log channel value.
131-
132-
</details>
133-
134-
<details>
135-
<summary>Administrator Tools</summary>
136-
137-
### Administrator Tools
138-
139-
Adds a few commands useful for admin operations.
140-
141-
#### !clear_message * Aliases: `cls, purge, delete`
142-
* Clear the specified number of messages from the current text channel.
143-
144-
#### !members
70+
5. Install all the requirements for python:
71+
```bash
72+
pip install -r requirements.txt
73+
```
74+
6. Run the bot:
75+
```bash
76+
python3 main.py
77+
```
78+
79+
## Current Functions
80+
The list below describes the different "Cogs" of the bot, their associated commands, and any additional information required to set them up.
81+
82+
<details>
83+
<summary>Voicemaster</summary>
84+
85+
### Voicemaster
86+
#### !setvmmaster <channel_id>
87+
* Make the given ID a Voicemaster master.
88+
89+
#### !getvmmasters * Get all the Voicemaster masters in the server.
90+
91+
#### !removevmmaster <channel_id>
92+
* Remove the given ID as a Voicemaster master.
93+
94+
#### !removeallmasters * Remove all Voicemaster masters from the server.
95+
96+
#### !killallslaves * Kill all the Voicemaster slave channels in the server.
97+
98+
#### !lockvm * Locks the Voicemaster slave you're currently in to the number of current members.
99+
100+
#### !unlockvm * Unlocks the Voicemaster slave you're currently in.
101+
</details>
102+
103+
<details>
104+
<summary>Default Role</summary>
105+
106+
### Default role
107+
#### !setdefaultroles <role_mention | role_id> * Sets the roles that the server gives to members when they join the server.
108+
109+
#### !getdefaultroles * Gets the current default roles set for the server.
110+
111+
#### !removedefaultroles * Removes the current default roles for the server.
112+
</details>
113+
114+
<details>
115+
<summary>Log Channel</summary>
116+
117+
### Log Channel
118+
#### !setlogchannel <channel_mention | channel_id> * Set the log channel to the #'ed channel or given role ID.
119+
120+
#### !getlogchannel * Gets the current log channel value.
121+
122+
#### !removelogchannel * Removes the current log channel value.
123+
</details>
124+
125+
<details>
126+
<summary>Administrator Tools</summary>
127+
128+
### Administrator Tools
129+
Adds a few commands useful for admin operations.
130+
#### !clear_message * Aliases: `cls, purge, delete`
131+
* Clear the specified number of messages from the current text channel.
132+
133+
#### !members
145134
* List the current number of members in the server.
146135

147136
#### !remove-cog \<cog name>

src/esportsbot/cogs/DefaultRoleCog.py

Lines changed: 77 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from discord.ext import commands
22
from esportsbot.base_functions import role_id_from_mention
33
from esportsbot.db_gateway import DBGatewayActions
4-
from esportsbot.models import Guild_info
4+
from esportsbot.models import Guild_info, Default_roles
5+
from esportsbot.base_functions import role_id_from_mention, send_to_log_channel
56

67

78
class DefaultRoleCog(commands.Cog):
@@ -14,15 +15,24 @@ async def on_member_join(self, member):
1415
guild = DBGatewayActions().get(Guild_info, guild_id=member.guild.id)
1516
if not guild:
1617
return
17-
18-
if guild.default_role_id:
19-
default_role = member.guild.get_role(guild.default_role_id)
20-
await member.add_roles(default_role)
18+
# Get all the default role for the server from database
19+
guild_default_roles = DBGatewayActions().list(Default_roles, guild_id=member.guild.id)
20+
# Check to see if any roles exist
21+
if guild_default_roles:
22+
# Create list of roles from database response
23+
apply_roles = [member.guild.get_role(role.role_id) for role in guild_default_roles]
24+
# Add all the roles to the user, we don't check if they're valid as we do this on input
25+
await member.add_roles(*apply_roles)
2126
await self.bot.adminLog(
2227
None,
2328
{
24-
"Cog": str(type(self)),
25-
"Message": f"{member.mention} has joined the server and received the {default_role.mention} role"
29+
"Cog":
30+
str(type(self)),
31+
"Message":
32+
self.STRINGS['default_role_join'].format(
33+
member_name=member.mention,
34+
role_ids=(' '.join(f'<@&{x.id}>' for x in apply_roles))
35+
)
2636
},
2737
guildID=member.guild.id
2838
)
@@ -31,76 +41,86 @@ async def on_member_join(self, member):
3141
None,
3242
{
3343
"Cog": str(type(self)),
34-
"Message": f"{member.mention} has joined the server"
44+
"Message": self.STRINGS['default_role_join_no_role'].format(member_name=member.mention)
3545
},
3646
guildID=member.guild.id
3747
)
3848

3949
@commands.command(
40-
name="setdefaultrole",
41-
usage="<role_id> or <@role>",
42-
help="Sets the role that the server gives to members when they join the server"
50+
name="setdefaultroles",
51+
usage="<@role> <@role> <@role> ...",
52+
help="Sets the roles that the server gives to members when they join the server"
4353
)
4454
@commands.has_permissions(administrator=True)
45-
async def setdefaultrole(self, ctx, given_role_id):
46-
cleaned_role_id = role_id_from_mention(given_role_id) if given_role_id else False
47-
if cleaned_role_id:
48-
guild = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id)
49-
50-
if not guild:
51-
db_item = Guild_info(guild_id=ctx.guild.id, default_role_id=cleaned_role_id)
52-
DBGatewayActions().create(db_item)
53-
else:
54-
guild.default_role_id = cleaned_role_id
55-
DBGatewayActions().update(guild)
56-
57-
await ctx.channel.send(self.STRINGS['default_role_set'].format(role_id=cleaned_role_id))
58-
default_role = ctx.author.guild.get_role(cleaned_role_id)
59-
await self.bot.adminLog(
60-
ctx.message,
61-
{
62-
"Cog":
63-
str(type(self)),
64-
"Message":
65-
self.STRINGS['default_role_set_log'].format(author=ctx.author.mention,
66-
role_mention=default_role.mention)
67-
}
68-
)
55+
async def setdefaultroles(self, ctx, *, args: str):
56+
role_list = args.split(" ")
57+
if len(role_list) == 0:
58+
await ctx.channel.send(self.STRINGS['default_roles_set_empty'])
6959
else:
70-
await ctx.channel.send(self.STRINGS['default_role_set_missing_params'])
60+
checked_roles = []
61+
checking_error = False
62+
# Loop through the roles to check the input formatting is correct and that roles exist
63+
for role in role_list:
64+
try:
65+
# Clean the inputted role to just the id
66+
cleaned_role_id = role_id_from_mention(role)
67+
# Obtain role object from the guild to check it exists
68+
role_obj = ctx.author.guild.get_role(cleaned_role_id)
69+
# Add role to array to add post checks
70+
checked_roles.append(cleaned_role_id)
71+
except Exception as err:
72+
print(err)
73+
checking_error = True
74+
if not checking_error:
75+
for role in checked_roles:
76+
DBGatewayActions().create(Default_roles(guild_id=ctx.author.guild.id, role_id=role))
77+
await ctx.channel.send(self.STRINGS['default_roles_set'].format(roles=args))
78+
await self.bot.adminLog(
79+
ctx.message,
80+
{
81+
"Cog": str(type(self)),
82+
"Message": self.STRINGS['default_roles_set_log'].format(author_mention=ctx.author.mention,
83+
roles=args)
84+
}
85+
)
86+
else:
87+
await ctx.channel.send(self.STRINGS['default_roles_set_error'])
7188

7289
@commands.command(
73-
name="getdefaultrole",
90+
name="getdefaultroles",
7491
usage="",
75-
help="Gets the role that the server gives to members when they join the server"
92+
help="Gets the roles that the server gives to members when they join the server"
7693
)
7794
@commands.has_permissions(administrator=True)
78-
async def getdefaultrole(self, ctx):
79-
guild = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id)
80-
if not guild:
81-
await ctx.channel.send(self.STRINGS['default_role_missing'])
82-
return
83-
84-
if guild.default_role_id:
85-
await ctx.channel.send(self.STRINGS['default_role_get'].format(role_id=guild.default_role_id))
95+
async def getdefaultroles(self, ctx):
96+
# Get all the default role for the server from database
97+
guild_default_roles = DBGatewayActions().list(Default_roles, guild_id=ctx.author.guild.id)
98+
# Check to see if any roles exist
99+
if guild_default_roles:
100+
# Create list of roles from database response
101+
apply_roles = [ctx.author.guild.get_role(role.role_id) for role in guild_default_roles]
102+
# Return all the default roles to the user
103+
await ctx.channel.send(
104+
self.STRINGS['default_role_get'].format(role_ids=(' '.join(f'<@&{x.id}>' for x in apply_roles)))
105+
)
86106
else:
87107
await ctx.channel.send(self.STRINGS['default_role_missing'])
88108

89109
@commands.command(
90-
name="removedefaultrole",
110+
name="removedefaultroles",
91111
usage="",
92-
help="Removes the role that the server gives to members when they join the server"
112+
help="Removes the roles that the server gives to members when they join the server"
93113
)
94114
@commands.has_permissions(administrator=True)
95-
async def removedefaultrole(self, ctx):
96-
guild = DBGatewayActions().get(Guild_info, guild_id=ctx.author.guild.id)
97-
if not guild:
98-
await ctx.channel.send(self.STRINGS['default_role_missing'])
99-
return
100-
101-
if guild.default_role_id:
102-
guild.default_role_id = None
103-
DBGatewayActions().update(guild)
115+
async def removedefaultroles(self, ctx):
116+
# Get all the default role for the server from database
117+
guild_default_roles = DBGatewayActions().list(Default_roles, guild_id=ctx.author.guild.id)
118+
# Check to see if any roles exist
119+
if guild_default_roles:
120+
for default_role in guild_default_roles:
121+
# Remove the current role
122+
DBGatewayActions().delete(default_role)
123+
# Return a response to the user
104124
await ctx.channel.send(self.STRINGS['default_role_removed'])
105125
await self.bot.adminLog(
106126
ctx.message,

src/esportsbot/models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ class Guild_info(base):
99
__tablename__ = 'guild_info'
1010
guild_id = Column(BigInteger, primary_key=True, nullable=False)
1111
log_channel_id = Column(BigInteger, nullable=True)
12-
default_role_id = Column(BigInteger, nullable=True)
12+
13+
14+
class Default_roles(base):
15+
__tablename__ = 'default_roles'
16+
default_roles_id = Column(BigInteger, primary_key=True, autoincrement=True, nullable=False)
17+
guild_id = Column(BigInteger, nullable=False)
18+
role_id = Column(BigInteger, nullable=False)
1319

1420

1521
class Pingable_polls(base):

src/esportsbot/user_strings.toml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ members = "This server has {member_count} members including me"
1818
no_version = "No version recorded"
1919

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

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

27-
default_role_get = "Default role is set to {role_id}"
30+
default_role_get = "Default role(s) are set to {role_ids}"
2831

29-
default_role_removed = "Default role has been removed"
32+
default_role_removed = "Default role(s) are removed"
3033
default_role_removed_log = "{author_mention} has removed the default role"
3134

3235
[music]

0 commit comments

Comments
 (0)