Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 78 additions & 1 deletion cogs/ReactForRole.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ def __init__(self, bot: discord.Client):
self.rfr_database_manager = ReactForRoleDBManager(KoalaBot.database_manager)
self.rfr_database_manager.create_tables()

@commands.Cog.listener()
async def on_guild_join(self, guild: discord.Guild):
"""
On bot joining guild, add this guild to the database of inline statuses.
:param guild: Guild KoalaBot just joined
"""
self.rfr_database_manager.create_tables()
self.rfr_database_manager.add_guild_inline_status(str(guild.id))

@commands.Cog.listener()
async def on_guild_leave(self, guild: discord.Guild):
"""
On bot leaving guild, remove this guild from the database of inline statuses.
:param guild: Guild KoalaBot just left
"""
self.rfr_database_manager.remove_guild_inline_status(str(guild.id))

@commands.check(KoalaBot.is_guild_channel)
@commands.check(KoalaBot.is_admin)
@commands.check(rfr_is_enabled)
Expand Down Expand Up @@ -191,6 +208,7 @@ async def rfr_create_message(self, ctx: commands.Context):
embed.set_footer(text="ReactForRole")
embed.set_thumbnail(
url="https://cdn.discordapp.com/attachments/737280260541907015/752024535985029240/discord1.png")
await self.set_inline(ctx, embed)
rfr_msg: discord.Message = await channel.send(embed=embed)
self.rfr_database_manager.add_rfr_message(ctx.guild.id, channel.id, rfr_msg.id)
await self.overwrite_channel_add_reaction_perms(ctx.guild, channel)
Expand Down Expand Up @@ -361,6 +379,7 @@ async def rfr_edit_inline(self, ctx: commands.Context):
guild: discord.Guild = ctx.guild
text_channels: List[discord.TextChannel] = guild.text_channels
guild_rfr_messages = self.rfr_database_manager.get_guild_rfr_messages(guild.id)
self.rfr_database_manager.update_guild_inline_status(guild.id, change_all)
for rfr_message in guild_rfr_messages:
channel: discord.TextChannel = discord.utils.get(text_channels, id=rfr_message[1])
msg: discord.Message = await channel.fetch_message(id=rfr_message[2])
Expand Down Expand Up @@ -525,7 +544,7 @@ async def rfr_add_roles_to_msg(self, ctx: commands.Context):
KoalaBot.logger.info(
f"ReactForRole: Added role ID {str(role.id)} to rfr message (channel, guild) {msg.id} "
f"({str(channel.id)}, {str(ctx.guild.id)}) with emoji {discord_emoji.id}.")

await self.set_inline(ctx, embed=rfr_embed)
await msg.edit(embed=rfr_embed)
await ctx.send("Okay, you should see the message with its new emojis now.")

Expand Down Expand Up @@ -980,6 +999,19 @@ def get_number_of_embed_fields(self, embed: discord.Embed) -> int:
"""
return len(embed.fields)

async def set_inline(self, ctx, embed):
"""
Sets the inline of all fields in an embed
:param ctx: Context of the embed
:param embed: The embed to be edited
"""
length = self.get_number_of_embed_fields(embed)
if length > 0:
for i in range(length):
field = embed.fields[i]
embed.set_field_at(i, name=field.name, value=field.value,
inline=self.rfr_database_manager.get_guild_inline_status(ctx.guild.id)[0] == 'Y')

async def get_first_emoji_from_str(self, ctx: commands.Context, content: str) -> Optional[
Union[discord.Emoji, str]]:
"""
Expand Down Expand Up @@ -1078,9 +1110,19 @@ def create_tables(self):
UNIQUE (guild_id, role_id)
);
"""
sql_create_inline_all_status_table = """
CREATE TABLE IF NOT EXISTS InlineAllStatus (
guild_id text NOT NULL,
inline_status text,
PRIMARY KEY (guild_id),
FOREIGN KEY (guild_id) REFERENCES GuildExtensions(guild_id),
UNIQUE (guild_id)
);
"""
self.database_manager.db_execute_commit(sql_create_guild_rfr_message_ids_table)
self.database_manager.db_execute_commit(sql_create_rfr_message_emoji_roles_table)
self.database_manager.db_execute_commit(sql_create_rfr_required_roles_table)
self.database_manager.db_execute_commit(sql_create_inline_all_status_table)

def add_rfr_message(self, guild_id: int, channel_id: int, message_id: int):
"""
Expand Down Expand Up @@ -1273,6 +1315,41 @@ def get_guild_rfr_required_roles(self, guild_id) -> List[int]:
return []
return role_ids

def add_guild_inline_status(self, guild_id):
"""
Add an inline status for all rfr messages in a guild
:param guild_id: guild ID
"""
self.database_manager.db_execute_commit(
"INSERT INTO InlineAllStatus (guild_id, inline_status) VALUES (?, ?);",
args=[str(guild_id), "N"])

def update_guild_inline_status(self, guild_id, status):
"""
Updates the inline status for all rfr messages in a guild
:param guild_id: guild ID
:param status: inline all status "Y" or "N"
"""
self.database_manager.db_execute_commit(
"UPDATE InlineAllStatus SET inline_status = ? WHERE guild_id = ?;", args=[status, str(guild_id)])

def get_guild_inline_status(self, guild_id):
"""
Gets the inline status for the specific guild
:param guild_id: guild ID
:return: Inline All Status for that guild
"""
return self.database_manager.db_execute_select("SELECT inline_status FROM InlineAllStatus WHERE guild_id = ?",
args=[str(guild_id)])[0]

def remove_guild_inline_status(self, guild_id):
"""
Removes the guild's inline status from the database
:param guild_id: guild ID
"""
self.database_manager.db_execute_commit("DELETE FROM InlineAllStatus WHERE guild_id = ?",
args=[str(guild_id)])


def setup(bot: KoalaBot) -> None:
"""
Expand Down