Skip to content

Commit

Permalink
plugin count_together
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikk155 committed Dec 6, 2024
1 parent b1c33a5 commit e0f85c7
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
5 changes: 4 additions & 1 deletion plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@
{
"script": "ping_counter.py",
"Disable": false
},
{
"script": "count_together.py",
"Disable": false
}
],
"$schema": "schema.json"
}

108 changes: 108 additions & 0 deletions plugins/count_together.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""
The MIT License (MIT)
Copyright (c) 2024 Mikk155
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""

from src.main import *

def on_initialization() -> dict:
'''
Called when the script is executed, this is the first hook ever called.
The bot is not even initialized yet.
This hook is required on all plugins and must return data.
'''

__data__: dict = {};
__data__["name"] = "Count together";
__data__["description"] = "React with ❌ to messages that did a wrong count";
__hooks__: list[Hooks] = [ Hooks.on_message ];
__data__["hooks"] = __hooks__;

return __data__;

@bot.tree.command()
@app_commands.guild_only()
@app_commands.describe( channel='Channel', number='Last number sent in the channel' )
async def cfg_count_together( interaction: discord.Interaction, channel: discord.TextChannel, number: int ):
"""Configure a channel as a count-together channel"""

await interaction.response.defer( thinking=True );

try:

cache = g_Cache.get();

cache[ str( interaction.guild_id ) ] = { "channel": channel.id, "number": number };

await interaction.followup.send(
g_Format.brackets(
g_Sentences.get(
"channel.configured",
interaction.guild_id
),
[
channel.jump_url
]
)
);

except Exception as e:

await bot.exception_handle( e, interaction=interaction );

async def on_message( message: discord.Message ) -> int:

if message.guild and message.content:

cache = g_Cache.get();

if str( message.guild.id ) in cache:

data = cache.get( str( message.guild.id ), {} );

if "channel" in data and data[ "channel" ] == message.channel.id:

num = re.search( r'\b(\d+)\b', message.content );

if num:

current = int( num.group(1) );

desired = data[ "number" ] + 1;

if desired != current:

await message.add_reaction( '❌' );

else:

data[ "number" ] = desired;

cache = data;

else:

await message.add_reaction( '❌' );

return HOOK_CONTINUE();
5 changes: 5 additions & 0 deletions sentences/__defs__.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@
{
"english": "Status",
"spanish": "Estatus"
},
"channel.configured":
{
"english": "Channel {} configured",
"spanish": "Canal {} configurado"
}
}

0 comments on commit e0f85c7

Please sign in to comment.