|
1 | | -from nextcord.ext import commands, tasks |
2 | | -import nextcord |
3 | | -import json |
4 | | -import os |
5 | | -import re |
6 | | -from bot.bot import bot |
7 | | -from shared.config import STAFF_ROLES |
8 | | - |
9 | | -class ModerationCog(commands.Cog): |
10 | | - """ |
11 | | - A cog dedicated to moderation tasks, including banning users based on username patterns. |
12 | | - """ |
13 | | - |
14 | | - def __init__(self, bot): |
15 | | - self.bot = bot |
16 | | - |
17 | | - def get_banned_users(self): |
18 | | - filepath = '/usr/src/app/data/banned_users.json' |
19 | | - if not os.path.exists(filepath): |
20 | | - os.makedirs(os.path.dirname(filepath), exist_ok=True) |
21 | | - with open(filepath, 'w') as file: |
22 | | - json.dump([], file) |
23 | | - with open(filepath, 'r') as file: |
24 | | - return json.load(file) |
25 | | - |
26 | | - def save_banned_users(self, data): |
27 | | - """Saves updated list of banned user patterns to the JSON file.""" |
28 | | - filepath = '/usr/src/app/data/banned_users.json' # Adjusted path |
29 | | - with open(filepath, 'w') as file: |
30 | | - json.dump(data, file) |
31 | | - |
32 | | - @bot.slash_command(name="add_ban_pattern", description="Add a username pattern to ban list.") |
33 | | - @commands.has_any_role(*STAFF_ROLES) |
34 | | - async def add_ban_pattern(self, interaction: nextcord.Interaction, pattern: str): |
35 | | - """Adds a new username pattern to the banned list.""" |
36 | | - banned_users = self.get_banned_users() |
37 | | - if pattern not in banned_users: |
38 | | - banned_users.append(pattern) |
39 | | - self.save_banned_users(banned_users) |
40 | | - await interaction.response.send_message(f"Pattern `{pattern}` added to banned list.", ephemeral=True) |
41 | | - else: |
42 | | - await interaction.response.send_message(f"Pattern `{pattern}` is already in the banned list.", ephemeral=True) |
43 | | - |
44 | | - @bot.slash_command(name="remove_ban_pattern", description="Remove a username pattern from the ban list.") |
45 | | - @commands.has_any_role(*STAFF_ROLES) |
46 | | - async def remove_ban_pattern(self, interaction: nextcord.Interaction, pattern: str): |
47 | | - """Removes a username pattern from the banned list.""" |
48 | | - banned_users = self.get_banned_users() |
49 | | - if pattern in banned_users: |
50 | | - banned_users.remove(pattern) |
51 | | - self.save_banned_users(banned_users) |
52 | | - await interaction.response.send_message(f"Pattern `{pattern}` removed from banned list.", ephemeral=True) |
53 | | - else: |
54 | | - await interaction.response.send_message(f"Pattern `{pattern}` is not in the banned list.", ephemeral=True) |
55 | | - |
56 | | - @bot.slash_command(name="list_ban_patterns", description="List all username patterns in the ban list.") |
57 | | - @commands.has_any_role(*STAFF_ROLES) |
58 | | - async def list_ban_patterns(self, interaction: nextcord.Interaction): |
59 | | - """Lists all username patterns in the banned list.""" |
60 | | - banned_users = self.get_banned_users() |
61 | | - if banned_users: |
62 | | - patterns = "\n".join(banned_users) |
63 | | - await interaction.response.send_message(f"Current banned patterns:\n```\n{patterns}\n```", ephemeral=True) |
64 | | - else: |
65 | | - await interaction.response.send_message("There are no patterns in the banned list.", ephemeral=True) |
66 | | - |
67 | | - @commands.Cog.listener() |
68 | | - async def on_member_join(self, member): |
69 | | - # This regex matches usernames with letters followed by an underscore and digits |
70 | | - pattern = re.compile(r'^[a-zA-Z]+_\d+$') |
71 | | - if pattern.match(member.name): |
72 | | - channel = self.bot.get_channel(1105538041021472900) # Make sure the ID is correct |
73 | | - view = BanConfirmationView(member.name.split('_')[0] + '_\d+') # Note the pattern change here for display purposes |
74 | | - |
75 | | - # Send the confirmation message with buttons |
76 | | - message_text = f"Would you like to add the pattern for `{member.name.split('_')[0]}` followed by any number of digits to the bot ban list?" |
77 | | - message = await channel.send(message_text, view=view) |
78 | | - |
79 | | - # Wait for the View to stop listening for interaction |
80 | | - await view.wait() |
81 | | - if view.value is True: |
82 | | - # Add pattern to ban list and update JSON if confirmed |
83 | | - banned_users = self.get_banned_users() |
84 | | - new_pattern = member.name.split('_')[0] + '_\\d+' # This is how you represent the pattern in the list |
85 | | - if new_pattern not in banned_users: |
86 | | - banned_users.append(new_pattern) |
87 | | - self.save_banned_users(banned_users) |
88 | | - await message.edit(content=f"Pattern `{new_pattern}` added to banned list.", view=None) |
89 | | - else: |
90 | | - await message.edit(content=f"Pattern `{new_pattern}` is already in the banned list.", view=None) |
91 | | - elif view.value is False: |
92 | | - # If declined, update the message |
93 | | - await message.edit(content="Pattern addition cancelled.", view=None) |
94 | | - |
95 | | - |
96 | | - @tasks.loop(hours=12) |
97 | | - async def routine_check(self, interaction: nextcord.Interaction): |
98 | | - """Performs a daily check on all server members against the banned username patterns.""" |
99 | | - banned_users = self.get_banned_users() |
100 | | - for member in interaction.guild.members: |
101 | | - for pattern in banned_users: |
102 | | - if re.match(pattern, member.name): |
103 | | - await member.ban(reason="Routine check: Matched banned pattern.") |
104 | | - break |
105 | | - |
106 | | - @bot.slash_command(name="force_bot_check", description="Performs an immediate check of all members against the ban list.") |
107 | | - @commands.has_any_role(*STAFF_ROLES) |
108 | | - async def force_check(self, interaction: nextcord.Interaction): |
109 | | - """Performs an immediate check against all members for banned username patterns.""" |
110 | | - await interaction.response.defer(ephemeral=True) # Acknowledge the command interaction |
111 | | - banned_users = self.get_banned_users() |
112 | | - count = 0 |
113 | | - for member in interaction.guild.members: |
114 | | - for pattern in banned_users: |
115 | | - if re.match(pattern, member.name): |
116 | | - await member.ban(reason="Ad-hoc check: Matched banned pattern.") |
117 | | - count += 1 |
118 | | - break # Break to avoid multiple checks if multiple patterns match |
119 | | - await interaction.followup.send(f"Ad-hoc check completed. Banned {count} user(s) matching the patterns.", ephemeral=True) |
120 | | - |
121 | | -# class BanConfirmationView(nextcord.ui.View): |
122 | | -# def __init__(self, pattern, *args, **kwargs): |
123 | | -# super().__init__(*args, **kwargs) |
124 | | -# self.pattern = pattern |
125 | | -# self.value = None # To track the staff's decision |
126 | | - |
127 | | -# @nextcord.ui.button(label='Yes', style=nextcord.ButtonStyle.green, emoji='👍') |
128 | | -# async def confirm(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): |
129 | | -# self.value = True |
130 | | -# self.stop() |
131 | | - |
132 | | -# @nextcord.ui.button(label='No', style=nextcord.ButtonStyle.red, emoji='👎') |
133 | | -# async def cancel(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): |
134 | | -# self.value = False |
135 | | -# self.stop() |
136 | | - |
137 | | -# async def update_message(self, interaction: nextcord.Interaction, content: str, button_label: str): |
138 | | -# # Create a new View with a disabled button |
139 | | -# new_view = nextcord.ui.View() |
140 | | -# button = nextcord.ui.Button(label=button_label, style=nextcord.ButtonStyle.grey, disabled=True) |
141 | | -# new_view.add_item(button) |
142 | | -# # Edit the original message with new content and the updated view |
143 | | -# await interaction.message.edit(content=content, view=new_view) |
144 | | - |
145 | | -# @nextcord.ui.button(label='Yes', style=nextcord.ButtonStyle.green, emoji='👍') |
146 | | -# async def confirm(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): |
147 | | -# self.value = True |
148 | | -# # Assuming you have logic here to actually add the user to the ban list |
149 | | -# await self.update_message(interaction, "This user has been added to the ban list.", "Added") |
150 | | -# self.stop() |
151 | | - |
152 | | -# @nextcord.ui.button(label='No', style=nextcord.ButtonStyle.red, emoji='👎') |
153 | | -# async def cancel(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): |
154 | | -# self.value = False |
155 | | -# await self.update_message(interaction, "This user has not been added to the ban list.", "Not Added") |
156 | | -# self.stop() |
157 | | - |
158 | | -def setup(bot): |
159 | | - bot.add_cog(ModerationCog(bot)) |
| 1 | +# from nextcord.ext import commands, tasks |
| 2 | +# import nextcord |
| 3 | +# import json |
| 4 | +# import os |
| 5 | +# import re |
| 6 | +# from bot.bot import bot |
| 7 | +# from shared.config import STAFF_ROLES |
| 8 | +# |
| 9 | +# class ModerationCog(commands.Cog): |
| 10 | +# """ |
| 11 | +# A cog dedicated to moderation tasks, including banning users based on username patterns. |
| 12 | +# """ |
| 13 | +# |
| 14 | +# def __init__(self, bot): |
| 15 | +# self.bot = bot |
| 16 | +# |
| 17 | +# def get_banned_users(self): |
| 18 | +# filepath = '/usr/src/app/data/banned_users.json' |
| 19 | +# if not os.path.exists(filepath): |
| 20 | +# os.makedirs(os.path.dirname(filepath), exist_ok=True) |
| 21 | +# with open(filepath, 'w') as file: |
| 22 | +# json.dump([], file) |
| 23 | +# with open(filepath, 'r') as file: |
| 24 | +# return json.load(file) |
| 25 | +# |
| 26 | +# def save_banned_users(self, data): |
| 27 | +# """Saves updated list of banned user patterns to the JSON file.""" |
| 28 | +# filepath = '/usr/src/app/data/banned_users.json' # Adjusted path |
| 29 | +# with open(filepath, 'w') as file: |
| 30 | +# json.dump(data, file) |
| 31 | +# |
| 32 | +# @bot.slash_command(name="add_ban_pattern", description="Add a username pattern to ban list.") |
| 33 | +# @commands.has_any_role(*STAFF_ROLES) |
| 34 | +# async def add_ban_pattern(self, interaction: nextcord.Interaction, pattern: str): |
| 35 | +# """Adds a new username pattern to the banned list.""" |
| 36 | +# banned_users = self.get_banned_users() |
| 37 | +# if pattern not in banned_users: |
| 38 | +# banned_users.append(pattern) |
| 39 | +# self.save_banned_users(banned_users) |
| 40 | +# await interaction.response.send_message(f"Pattern `{pattern}` added to banned list.", ephemeral=True) |
| 41 | +# else: |
| 42 | +# await interaction.response.send_message(f"Pattern `{pattern}` is already in the banned list.", ephemeral=True) |
| 43 | +# |
| 44 | +# @bot.slash_command(name="remove_ban_pattern", description="Remove a username pattern from the ban list.") |
| 45 | +# @commands.has_any_role(*STAFF_ROLES) |
| 46 | +# async def remove_ban_pattern(self, interaction: nextcord.Interaction, pattern: str): |
| 47 | +# """Removes a username pattern from the banned list.""" |
| 48 | +# banned_users = self.get_banned_users() |
| 49 | +# if pattern in banned_users: |
| 50 | +# banned_users.remove(pattern) |
| 51 | +# self.save_banned_users(banned_users) |
| 52 | +# await interaction.response.send_message(f"Pattern `{pattern}` removed from banned list.", ephemeral=True) |
| 53 | +# else: |
| 54 | +# await interaction.response.send_message(f"Pattern `{pattern}` is not in the banned list.", ephemeral=True) |
| 55 | +# |
| 56 | +# @bot.slash_command(name="list_ban_patterns", description="List all username patterns in the ban list.") |
| 57 | +# @commands.has_any_role(*STAFF_ROLES) |
| 58 | +# async def list_ban_patterns(self, interaction: nextcord.Interaction): |
| 59 | +# """Lists all username patterns in the banned list.""" |
| 60 | +# banned_users = self.get_banned_users() |
| 61 | +# if banned_users: |
| 62 | +# patterns = "\n".join(banned_users) |
| 63 | +# await interaction.response.send_message(f"Current banned patterns:\n```\n{patterns}\n```", ephemeral=True) |
| 64 | +# else: |
| 65 | +# await interaction.response.send_message("There are no patterns in the banned list.", ephemeral=True) |
| 66 | +# |
| 67 | +# @commands.Cog.listener() |
| 68 | +# async def on_member_join(self, member): |
| 69 | +# # This regex matches usernames with letters followed by an underscore and digits |
| 70 | +# pattern = re.compile(r'^[a-zA-Z]+_\d+$') |
| 71 | +# if pattern.match(member.name): |
| 72 | +# channel = self.bot.get_channel(1105538041021472900) # Make sure the ID is correct |
| 73 | +# view = BanConfirmationView(member.name.split('_')[0] + '_\d+') # Note the pattern change here for display purposes |
| 74 | +# |
| 75 | +# # Send the confirmation message with buttons |
| 76 | +# message_text = f"Would you like to add the pattern for `{member.name.split('_')[0]}` followed by any number of digits to the bot ban list?" |
| 77 | +# message = await channel.send(message_text, view=view) |
| 78 | +# |
| 79 | +# # Wait for the View to stop listening for interaction |
| 80 | +# await view.wait() |
| 81 | +# if view.value is True: |
| 82 | +# # Add pattern to ban list and update JSON if confirmed |
| 83 | +# banned_users = self.get_banned_users() |
| 84 | +# new_pattern = member.name.split('_')[0] + '_\\d+' # This is how you represent the pattern in the list |
| 85 | +# if new_pattern not in banned_users: |
| 86 | +# banned_users.append(new_pattern) |
| 87 | +# self.save_banned_users(banned_users) |
| 88 | +# await message.edit(content=f"Pattern `{new_pattern}` added to banned list.", view=None) |
| 89 | +# else: |
| 90 | +# await message.edit(content=f"Pattern `{new_pattern}` is already in the banned list.", view=None) |
| 91 | +# elif view.value is False: |
| 92 | +# # If declined, update the message |
| 93 | +# await message.edit(content="Pattern addition cancelled.", view=None) |
| 94 | +# |
| 95 | +# |
| 96 | +# @tasks.loop(hours=12) |
| 97 | +# async def routine_check(self, interaction: nextcord.Interaction): |
| 98 | +# """Performs a daily check on all server members against the banned username patterns.""" |
| 99 | +# banned_users = self.get_banned_users() |
| 100 | +# for member in interaction.guild.members: |
| 101 | +# for pattern in banned_users: |
| 102 | +# if re.match(pattern, member.name): |
| 103 | +# await member.ban(reason="Routine check: Matched banned pattern.") |
| 104 | +# break |
| 105 | +# |
| 106 | +# @bot.slash_command(name="force_bot_check", description="Performs an immediate check of all members against the ban list.") |
| 107 | +# @commands.has_any_role(*STAFF_ROLES) |
| 108 | +# async def force_check(self, interaction: nextcord.Interaction): |
| 109 | +# """Performs an immediate check against all members for banned username patterns.""" |
| 110 | +# await interaction.response.defer(ephemeral=True) # Acknowledge the command interaction |
| 111 | +# banned_users = self.get_banned_users() |
| 112 | +# count = 0 |
| 113 | +# for member in interaction.guild.members: |
| 114 | +# for pattern in banned_users: |
| 115 | +# if re.match(pattern, member.name): |
| 116 | +# await member.ban(reason="Ad-hoc check: Matched banned pattern.") |
| 117 | +# count += 1 |
| 118 | +# break # Break to avoid multiple checks if multiple patterns match |
| 119 | +# await interaction.followup.send(f"Ad-hoc check completed. Banned {count} user(s) matching the patterns.", ephemeral=True) |
| 120 | +# |
| 121 | +# # class BanConfirmationView(nextcord.ui.View): |
| 122 | +# # def __init__(self, pattern, *args, **kwargs): |
| 123 | +# # super().__init__(*args, **kwargs) |
| 124 | +# # self.pattern = pattern |
| 125 | +# # self.value = None # To track the staff's decision |
| 126 | +# |
| 127 | +# # @nextcord.ui.button(label='Yes', style=nextcord.ButtonStyle.green, emoji='👍') |
| 128 | +# # async def confirm(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): |
| 129 | +# # self.value = True |
| 130 | +# # self.stop() |
| 131 | +# |
| 132 | +# # @nextcord.ui.button(label='No', style=nextcord.ButtonStyle.red, emoji='👎') |
| 133 | +# # async def cancel(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): |
| 134 | +# # self.value = False |
| 135 | +# # self.stop() |
| 136 | +# |
| 137 | +# # async def update_message(self, interaction: nextcord.Interaction, content: str, button_label: str): |
| 138 | +# # # Create a new View with a disabled button |
| 139 | +# # new_view = nextcord.ui.View() |
| 140 | +# # button = nextcord.ui.Button(label=button_label, style=nextcord.ButtonStyle.grey, disabled=True) |
| 141 | +# # new_view.add_item(button) |
| 142 | +# # # Edit the original message with new content and the updated view |
| 143 | +# # await interaction.message.edit(content=content, view=new_view) |
| 144 | +# |
| 145 | +# # @nextcord.ui.button(label='Yes', style=nextcord.ButtonStyle.green, emoji='👍') |
| 146 | +# # async def confirm(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): |
| 147 | +# # self.value = True |
| 148 | +# # # Assuming you have logic here to actually add the user to the ban list |
| 149 | +# # await self.update_message(interaction, "This user has been added to the ban list.", "Added") |
| 150 | +# # self.stop() |
| 151 | +# |
| 152 | +# # @nextcord.ui.button(label='No', style=nextcord.ButtonStyle.red, emoji='👎') |
| 153 | +# # async def cancel(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): |
| 154 | +# # self.value = False |
| 155 | +# # await self.update_message(interaction, "This user has not been added to the ban list.", "Not Added") |
| 156 | +# # self.stop() |
| 157 | +# |
| 158 | +# def setup(bot): |
| 159 | +# bot.add_cog(ModerationCog(bot)) |
0 commit comments