From 49cc0f41319b7de5567497376e88ff830fd77934 Mon Sep 17 00:00:00 2001 From: ggodot Date: Fri, 10 Jan 2025 22:58:52 +0100 Subject: [PATCH] language support --- bot.py | 51 ++++++++++++++++++++++++++++++++++++--------------- bot.service | 8 ++++---- script.sh | 10 +++++++--- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/bot.py b/bot.py index c239f70..e30a1a6 100644 --- a/bot.py +++ b/bot.py @@ -16,8 +16,14 @@ async def on_ready(): @bot.slash_command(guild_ids=g_ids, name = "summary", description="Returns a Wikipedia summary") -async def summary(ctx, search: Option(str, description="What do you want to get summary for?", required = True)): - await ctx.channel.trigger_typing() # shows that bot is typing +async def summary(ctx, search: Option(str, description="What do you want to get summary for?", required = True), lang: Option(str, description="Language of wiki", required = False) = "en"): + await ctx.channel.trigger_typing() # that bot is typing + + try: + wikipedia.set_lang(lang) + except: + await ctx.author.send("Your chosen language doesnt have dedicated wiki") + try: # sezene sumar a odesle thesummary = wikipedia.summary(search, chars = 1950) try: @@ -26,16 +32,19 @@ async def summary(ctx, search: Option(str, description="What do you want to get await ctx.send(thesummary) # Send horsi, pouzije pokud nestihne response except: # pokud nesezena musi vyhledavat searchsummary = str(wikipedia.search(search, suggestion = True)).replace('(', '').replace(")", "").replace("'", "").replace('[', '').replace(']', '') - try: - await ctx.respond(f"I can't seem to find a summary for that.. DId you mean: {searchsummary}") - except: - await ctx.send(f"I can't seem to find a summary for that.. DId you mean: {searchsummary}") + await ctx.author.send(f"I can't seem to find a summary for that.. Did you mean: {searchsummary}") @bot.slash_command(guild_ids=g_ids, name = "search", description="Search Wikipedia") -async def search(ctx, search: Option(str, description = "What do you want to search for?", required = True)): +async def search(ctx, search: Option(str, description = "What do you want to search for?", required = True), lang: Option(str, description="Language of wiki", required = False) = "en"): await ctx.channel.trigger_typing() # shows that bot is typing + + try: + wikipedia.set_lang(lang) + except: + await ctx.author.send("Your chosen language doesnt have dedicated wiki") + searchsearch = str(wikipedia.search(search, suggestion = True)).replace('(', '').replace(")", "").replace("'", "").replace('[', '').replace(']', '') try: await ctx.respond(searchsearch) @@ -45,33 +54,45 @@ async def search(ctx, search: Option(str, description = "What do you want to sea @bot.slash_command(guild_ids=g_ids, name = "url", description="Get a URL to a page on Wikipedia") -async def search(ctx, search: Option(str, description = "What do you want to get URL for?", required = True)): +async def url(ctx, search: Option(str, description = "What do you want to get URL for?", required = True), lang: Option(str, description="Language of wiki", required = False) = "en"): await ctx.channel.trigger_typing() # shows that bot is typing + + try: + wikipedia.set_lang(lang) + except: + await ctx.author.send("Your chosen language doesnt have dedicated wiki") + try: # pokud sezenu sumary tak sezenu link urlsummary = wikipedia.summary(search, auto_suggest = False) search = search.lower().replace(" ","_").replace(" ","_") try: - await ctx.respond(f"https://en.wikipedia.org/wiki/{search}") + await ctx.respond(f"https://{lang}.wikipedia.org/wiki/{search}") except: - await ctx.send(f"https://en.wikipedia.org/wiki/{search}") + await ctx.send(f"https://{lang}.wikipedia.org/wiki/{search}") except: urlsearch = str(wikipedia.search(search, suggestion = True)).replace('(', '').replace(")", "").replace("'", "").replace('[', '').replace(']', '') try: - await ctx.respond(f"https://en.wikipedia.org/wiki/{urlsearch}") + await ctx.respond(f"https://{lang}.wikipedia.org/wiki/{urlsearch}") except: - await ctx.send(f"https://en.wikipedia.org/wiki/{urlsearch}") + await ctx.send(f"https://{lang}.wikipedia.org/wiki/{urlsearch}") @bot.slash_command(guild_ids=g_ids, name = "random", description="Returns a random Wikipedia article") -async def search(ctx): +async def random(ctx, lang: Option(str, description = "Language of wiki", required = False) = "en"): await ctx.channel.trigger_typing() # shows that bot is typing + + try: + wikipedia.set_lang(lang) + except: + await ctx.author.send("Your chosen language doesnt have dedicated wiki") + ran_article = wikipedia.random().replace(" ", "_").replace(" ", "_") try: - await ctx.respond(f"https://en.wikipedia.org/wiki/{ran_article}") + await ctx.respond(f"https://{lang}.wikipedia.org/wiki/{ran_article}") except: - await ctx.send(f"https://en.wikipedia.org/wiki/{ran_article}") + await ctx.send(f"https://{lang}.wikipedia.org/wiki/{ran_article}") diff --git a/bot.service b/bot.service index b6b12dc..b0f9dbf 100644 --- a/bot.service +++ b/bot.service @@ -5,8 +5,8 @@ After=syslog.target [Service] Type=simple -User=disbot -Group=disbot +User=bot +Group=bot ProtectControlGroups=true ProtectHome=true @@ -17,9 +17,9 @@ ProtectSystem=full PrivateDevices=true PrivateUsers=true -WorkingDirectory=/opt/disbot/ +WorkingDirectory=/opt/bot/ -ExecStart=/opt/disbot/env/bin/python3 /opt/disbot/disbot.py +ExecStart=/opt/bot/env/bin/python3 /opt/bot/bot.py Restart=on-failure diff --git a/script.sh b/script.sh index d15d48a..54e388a 100755 --- a/script.sh +++ b/script.sh @@ -19,11 +19,15 @@ mkdir /opt/bot/env/ python3 -m venv /opt/bot/env/ source /opt/bot/env/bin/activate -pip install -r req.txt +pip install discord +pip install py-cord +pip uninstall discord +pip install wikipedia + deactivate -mv bot.py /opt/disbot/ -mkdir /opt/disbot/backup/ +mv bot.py /opt/bot/ +mkdir /opt/bot/backup/ chown -R bot:bot /opt/bot/