Skip to content

Commit

Permalink
Merge branch 'master' of github.com:asso-msn/MechaDon2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tina-otoge committed Jun 3, 2024
2 parents 6045c10 + 34c57d9 commit d9cb7f9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
32 changes: 32 additions & 0 deletions alembic/versions/0b579352511c_add_announce_channel_to_state_cog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Add announce channel to state cog
Revision ID: 0b579352511c
Revises: a634d3ec8146
Create Date: 2024-05-15 22:55:36.794539
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '0b579352511c'
down_revision = 'a634d3ec8146'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('states', schema=None) as batch_op:
batch_op.add_column(sa.Column('announce_channel_id', sa.Integer(), nullable=True))

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('states', schema=None) as batch_op:
batch_op.drop_column('announce_channel_id')

# ### end Alembic commands ###
19 changes: 17 additions & 2 deletions mechadon/cogs/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class State(db.Base):
channel_text = db.Column(db.String)
message_id = db.Column(db.Id)
message_text = db.Column(db.String)
announce_channel_id = db.Column(db.Id)

def __str__(self):
return f"{self.__class__.__name__} {self.react}"
Expand Down Expand Up @@ -50,6 +51,13 @@ async def on_raw_reaction_add(self, payload: RawReactionActionEvent):
channel = await self.bot.fetch_channel(state.channel_id)
await channel.edit(name=state.channel_text)

if state.announce_channel_id:
channel = await self.bot.fetch_channel(state.announce_channel_id)
author = channel.guild.get_member(payload.user_id)
await channel.send(
f"{author.display_name} changed the state to {state.react}"
)

if state.icon:
guild = self.bot.get_guild(payload.guild_id)
content = requests.get(state.icon).content
Expand Down Expand Up @@ -88,6 +96,9 @@ async def edit_state(self, context, react, key, value):
await self.reply(context, action, state)

async def update_state(self, context, state, key, value):
def sanitize_id(value):
return value.split("/")[-1].strip("<>#")

if key == "icon":
files = context.message.embeds + context.message.attachments
if files:
Expand All @@ -96,16 +107,20 @@ async def update_state(self, context, state, key, value):
value = value and value.strip("<>")

if key == "react":
value = value.split("/")[-1]
value = sanitize_id(value)
key = "react_message_id"

if key == "channel":
value = value.split("/")[-1]
value = sanitize_id(value)
key = "channel_id"

if key == "name":
key = "channel_text"

if key == "announce":
value = sanitize_id(value)
key = "announce_channel_id"

setattr(state, key, value)
db.session.commit()
await self.reply(context, "Updated", state, key, "->", value)

0 comments on commit d9cb7f9

Please sign in to comment.