language support

This commit is contained in:
ggodot 2025-01-10 22:58:52 +01:00
parent 45664d0567
commit 49cc0f4131
3 changed files with 47 additions and 22 deletions

51
bot.py
View File

@ -16,8 +16,14 @@ async def on_ready():
@bot.slash_command(guild_ids=g_ids, name = "summary", description="Returns a Wikipedia summary") @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)): 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() # shows that bot is typing 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 try: # sezene sumar a odesle
thesummary = wikipedia.summary(search, chars = 1950) thesummary = wikipedia.summary(search, chars = 1950)
try: 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 await ctx.send(thesummary) # Send horsi, pouzije pokud nestihne response
except: # pokud nesezena musi vyhledavat except: # pokud nesezena musi vyhledavat
searchsummary = str(wikipedia.search(search, suggestion = True)).replace('(', '').replace(")", "").replace("'", "").replace('[', '').replace(']', '') searchsummary = str(wikipedia.search(search, suggestion = True)).replace('(', '').replace(")", "").replace("'", "").replace('[', '').replace(']', '')
try: await ctx.author.send(f"I can't seem to find a summary for that.. Did you mean: {searchsummary}")
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}")
@bot.slash_command(guild_ids=g_ids, name = "search", description="Search Wikipedia") @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 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(']', '') searchsearch = str(wikipedia.search(search, suggestion = True)).replace('(', '').replace(")", "").replace("'", "").replace('[', '').replace(']', '')
try: try:
await ctx.respond(searchsearch) 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") @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 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 try: # pokud sezenu sumary tak sezenu link
urlsummary = wikipedia.summary(search, auto_suggest = False) urlsummary = wikipedia.summary(search, auto_suggest = False)
search = search.lower().replace(" ","_").replace(" ","_") search = search.lower().replace(" ","_").replace(" ","_")
try: try:
await ctx.respond(f"https://en.wikipedia.org/wiki/{search}") await ctx.respond(f"https://{lang}.wikipedia.org/wiki/{search}")
except: except:
await ctx.send(f"https://en.wikipedia.org/wiki/{search}") await ctx.send(f"https://{lang}.wikipedia.org/wiki/{search}")
except: except:
urlsearch = str(wikipedia.search(search, suggestion = True)).replace('(', '').replace(")", "").replace("'", "").replace('[', '').replace(']', '') urlsearch = str(wikipedia.search(search, suggestion = True)).replace('(', '').replace(")", "").replace("'", "").replace('[', '').replace(']', '')
try: try:
await ctx.respond(f"https://en.wikipedia.org/wiki/{urlsearch}") await ctx.respond(f"https://{lang}.wikipedia.org/wiki/{urlsearch}")
except: 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") @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 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(" ", "_") ran_article = wikipedia.random().replace(" ", "_").replace(" ", "_")
try: try:
await ctx.respond(f"https://en.wikipedia.org/wiki/{ran_article}") await ctx.respond(f"https://{lang}.wikipedia.org/wiki/{ran_article}")
except: except:
await ctx.send(f"https://en.wikipedia.org/wiki/{ran_article}") await ctx.send(f"https://{lang}.wikipedia.org/wiki/{ran_article}")

View File

@ -5,8 +5,8 @@ After=syslog.target
[Service] [Service]
Type=simple Type=simple
User=disbot User=bot
Group=disbot Group=bot
ProtectControlGroups=true ProtectControlGroups=true
ProtectHome=true ProtectHome=true
@ -17,9 +17,9 @@ ProtectSystem=full
PrivateDevices=true PrivateDevices=true
PrivateUsers=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 Restart=on-failure

View File

@ -19,11 +19,15 @@ mkdir /opt/bot/env/
python3 -m venv /opt/bot/env/ python3 -m venv /opt/bot/env/
source /opt/bot/env/bin/activate 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 deactivate
mv bot.py /opt/disbot/ mv bot.py /opt/bot/
mkdir /opt/disbot/backup/ mkdir /opt/bot/backup/
chown -R bot:bot /opt/bot/ chown -R bot:bot /opt/bot/