Skip to content

Commit

Permalink
Merge branch 'i18n' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
geirawsm committed Sep 7, 2024
2 parents 98020f1 + c08c670 commit 7cb0c58
Show file tree
Hide file tree
Showing 42 changed files with 2,872 additions and 1,110 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ aiosqlite = "*"
"colorgram.py" = "*"
spotipy = "*"
aiofiles = "*"
i18nice = {extras = ["yaml"], version = "*"}

[dev-packages]
requests-testadapter = "*"
Expand Down
658 changes: 367 additions & 291 deletions Pipfile.lock

Large diffs are not rendered by default.

164 changes: 131 additions & 33 deletions sausage_bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,33 @@
"Set's up the bot, have a few generic commands and controls cogs"
import discord
from discord.ext import commands
from discord.app_commands import locale_str
import os
import asyncio
from tabulate import tabulate

from sausage_bot.util.args import args
from sausage_bot.util import config, envs, file_io, cogs, db_helper
from sausage_bot.util import discord_commands
from sausage_bot.util.i18n import I18N, available_languages, set_language
from sausage_bot.util.i18n import MyTranslator
from sausage_bot.util.log import log


async def locales_autocomplete(
interaction: discord.Interaction,
current: str,
) -> list[discord.app_commands.Choice[str]]:
locales = available_languages()
log.debug(f'locales: {locales}')
return [
discord.app_commands.Choice(
name=locale, value=locale
)
for locale in locales if current.lower() in locale.lower()
]


# Create necessary folders before starting
check_and_create_folders = [
envs.DB_DIR,
Expand Down Expand Up @@ -42,10 +59,27 @@ async def on_ready():
When the bot is ready, it will notify in the log.
#autodoc skip#
'''
# Create locale db if not exists
log.verbose('Checking locale db')
await db_helper.prep_table(
table_in=envs.locale_db_schema,
old_inserts=['en']
)
locale_db = await db_helper.get_output(
template_info=envs.locale_db_schema,
single=True
)
log.debug(f'Setting locale to `{locale_db}`')
I18N.set('locale', locale_db)
await config.bot.tree.set_translator(MyTranslator())
for guild in config.bot.guilds:
if guild.name == config.env('DISCORD_GUILD'):
log.log('{} has connected to `{}`'.format(
config.bot.user, guild.name))
log.log(
I18N.t('main.msg.bot_connected',
bot=config.bot.user,
server=guild.name
)
)
break

log.verbose('Checking cog tasks db')
Expand All @@ -68,7 +102,7 @@ async def on_ready():
type=discord.ActivityType.watching,
name=config.env(
'BOT_WATCHING',
default='some random youtube video'
default=I18N.t('main.msg.bot_watching')
)
)
)
Expand All @@ -85,15 +119,22 @@ async def on_ready():
}
channel_out = await guild.create_text_channel(
name=str(bot_channel),
topic=f'Incoming log messages from {config.bot.user.name}',
topic=I18N.t('main.msg.create_log_channel_logging', botname=config.bot.user.name),
overwrites=overwrites
)
channel_out.set_permissions()


sync_group = discord.app_commands.Group(
name="sync", description=locale_str(
I18N.t('stats.commands.groups.stats')
)
)


@commands.check_any(commands.is_owner())
@config.bot.tree.command(
name='syncglobal', description='Owner only'
@sync_group.command(
name='global', description=locale_str(I18N.t('main.owner_only'))
)
async def sync_global(interaction: discord.Interaction):
await config.bot.tree.sync()
Expand All @@ -108,6 +149,7 @@ async def sync_global(interaction: discord.Interaction):
if _cmd != '':
_cmd += '\n'
await interaction.response.send_message(
# TODO i18n
f'Commands synched!\n{_cmd}',
ephemeral=True
)
Expand All @@ -116,7 +158,7 @@ async def sync_global(interaction: discord.Interaction):

@commands.is_owner()
@config.bot.tree.command(
name='syncdev', description='Owner only'
name='syncdev', description=locale_str(I18N.t('main.owner_only')),
)
async def sync_dev(interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
Expand Down Expand Up @@ -180,7 +222,7 @@ async def clear_commands(ctx):

@commands.is_owner()
@config.bot.tree.command(
name='version', description='Owner only'
name='version', description=locale_str(I18N.t('main.owner_only'))
)
async def get_version(interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
Expand All @@ -200,7 +242,7 @@ async def get_version(interaction: discord.Interaction):
# Commands
@commands.is_owner()
@config.bot.tree.command(
name='ping', description='Sjekk latency'
name='ping', description=locale_str(I18N.t('main.commands.ping.command'))
)
async def ping(interaction: discord.Interaction):
'Checks the bot latency'
Expand All @@ -216,22 +258,22 @@ async def ping(interaction: discord.Interaction):
)
@config.bot.tree.command(
name='delete',
description='Delete `amount` number of messages in the chat'
description=locale_str(I18N.t('main.commands.delete.command'))
)
async def delete(interaction: discord.Interaction, amount: int):
'Delete `amount` number of messages in the chat'
if amount <= 0:
await interaction.response.send_message(
'The number must be bigger than 0',
ephemeral=True
I18N.t('main.commands.delete.less_than_0'),
)
else:
await interaction.response.defer(ephemeral=True)
await interaction.channel.purge(
limit=amount, reason='Massesletting via bot'
limit=amount, reason=I18N.t('main.commands.delete.log_confirm')
)
await interaction.followup.send(
f'Deleted {amount} messages',
I18N.t('main.commands.delete.msg_confirm',
amount=amount),
ephemeral=True
)
return
Expand All @@ -243,7 +285,7 @@ async def delete(interaction: discord.Interaction, amount: int):
)
@config.bot.tree.command(
name='kick',
description='Kick a user with reason'
description=locale_str(I18N.t('main.commands.kick.command'))
)
async def kick(
interaction: discord.Interaction, member: discord.Member = None,
Expand All @@ -263,12 +305,18 @@ async def kick(
try:
await member.kick(reason=reason)
await interaction.followup.send(
f'{member} has been kicked',
I18N.t(
'main.commands.kick.msg_confirm',
member=member
),
ephemeral=True
)
except Exception as failkick:
except Exception as _error:
await interaction.followup.send(
f'Failed to kick: {failkick}',
I18N.t(
'main.commands.kick.msg_failed',
error=_error
),
ephemeral=True
)

Expand All @@ -279,7 +327,7 @@ async def kick(
)
@config.bot.tree.command(
name='ban',
description='Ban a user with reason'
description=locale_str(I18N.t('main.commands.ban.command'))
)
async def ban(
interaction: discord.Interaction, member: discord.Member = None,
Expand All @@ -299,20 +347,25 @@ async def ban(
try:
await member.ban(reason=reason)
await interaction.followup.send(
f'{member} has been banned',
I18N.t(
'main.commands.ban.msg_confirm',
member=member,
),
ephemeral=True
)
except Exception as failban:
except Exception as _error:
await interaction.followup.send(
f'Failed to ban: {failban}',
I18N.t(
'main.commands.ban.msg_failed',
error=_error
),
ephemeral=True
)


@commands.check_any(commands.is_owner())
@config.bot.tree.command(
name='say', description='Sender melding til en kanal. Fyll inn '
'`message_id` hvis det skal svares på en melding'
name='say', description=locale_str(I18N.t('main.commands.say.command'))
)
async def say(
interaction: discord.Interaction, channel: discord.TextChannel,
Expand All @@ -331,17 +384,27 @@ async def say(
else:
await channel.send(message)
await interaction.followup.send(
f'Melding sent til `#{channel.name}`', ephemeral=True
I18N.t(
'main.commands.say.msg_confirm',
channel=channel.name
),
ephemeral=True
)
except discord.Forbidden:
await interaction.followup.send(
'Jeg har ikke tilgang til å sende melding '
f'i `#{channel.name}`.',
I18N.t(
'main.commands.say.msg_forbidden',
channel=channel.name
),
ephemeral=True
)
except Exception as e:
except Exception as _error:
await interaction.followup.send(
f'An error occurred: {e}', ephemeral=True
I18N.t(
'main.commands.say.msg_error',
error=_error
),
ephemeral=True
)


Expand All @@ -351,12 +414,13 @@ async def say(
)
@config.bot.tree.command(
name='sayagain',
description='Endre en tidligere melding sendt med /say'
description=locale_str(I18N.t('main.commands.say_again.command'))
)
async def say_again(
interaction: discord.Interaction, msg_id: str, *, text: str
):
'Make the bot rephrase a previous message'
# TODO Legg til modal her
await interaction.response.defer(ephemeral=True)
guild = discord_commands.get_guild()
if guild is None:
Expand Down Expand Up @@ -384,7 +448,7 @@ async def say_again(
commands.has_permissions(administrator=True)
)
@config.bot.tree.command(
name="tasks", description="List tasks and their status"
name="tasks", description=locale_str(I18N.t('main.commands.tasks.command'))
)
async def get_tasks_list(interaction: discord.Interaction):
'''
Expand All @@ -410,7 +474,41 @@ async def get_tasks_list(interaction: discord.Interaction):
return


@commands.check_any(commands.is_owner())
@config.bot.tree.command(
name='language', description=locale_str(I18N.t('main.owner_only'))
)
@discord.app_commands.autocomplete(language=locales_autocomplete)
async def language(
interaction: discord.Interaction, language: str
):
await interaction.response.defer(ephemeral=True)
log.verbose(f'Setting language to {language}')
await set_language(language)
log.verbose('Syncing commands')
await config.bot.tree.sync()
await interaction.followup.send(
I18N.t(
'main.commands.language.confirm_language_set',
language=language
),
ephemeral=True)
return


@commands.check_any(commands.is_owner())
@config.bot.tree.command(
name='test', description=locale_str(I18N.t('main.owner_only'))
)
async def test(interaction: discord.Interaction):
await interaction.response.send_message(
I18N.t('dilemmas.commands.count.msg_confirm', count=0),
ephemeral=True
)
return


try:
config.bot.run(config.DISCORD_TOKEN)
except Exception as e:
log.error(f'Could not start bot: {e}')
except Exception as _error:
log.error(f'Could not start bot: {_error}')
Loading

0 comments on commit 7cb0c58

Please sign in to comment.