WikipeBot/bot.py
2025-02-12 16:16:04 +01:00

169 lines
6.1 KiB
Python

import discord, wikipedia, json
from discord import Option
import asyncio
from random import randrange
#from private.config import token
#import os
#os.chdir("/opt/disbot")
bot = discord.Bot()
g_ids = [1261698917163077632]
@bot.event
async def on_ready():
print(f"We've logged in as {bot.user}.")
@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), 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:
await ctx.respond(thesummary) # Respons je limitovano 3 vterinami, lepsi ale obcas nestihne
except:
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(']', '')
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), 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)
except:
await ctx.send(searchsearch)
@bot.slash_command(guild_ids=g_ids, name = "url", description="Get a URL to a page on Wikipedia")
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://{lang}.wikipedia.org/wiki/{search}")
except:
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://{lang}.wikipedia.org/wiki/{urlsearch}")
except:
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 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://{lang}.wikipedia.org/wiki/{ran_article}")
except:
await ctx.send(f"https://{lang}.wikipedia.org/wiki/{ran_article}")
@bot.slash_command(guild_ids=g_ids, name = "addfact", description="Adds fun fact into pool")
async def new_joke(ctx, fact: Option(str, description = "Text of the fun fact", required = True)):
await ctx.channel.trigger_typing()
with open("funfacts.json", "r") as f:
jokes = json.load(f)
new = {}
new["author"] = str(ctx.author.id)
new["upvotes"] = 0
new["downvotes"] = 0
new["visited"] = 0
new["txt"] = str(fact)
jokes["funfacts"].append(new)
try:
await ctx.respond(f"~ moc děkuju Senpai ~ UwU")
except:
await ctx.send(f"~ moc děkuju Senpai ~ UwU")
with open("funfacts.json", "w") as f:
json.dump(jokes, f)
return True
@bot.slash_command(guild_ids=g_ids, name = "funfact", description="Reads facts from the pool")
async def funfact(ctx):
await ctx.channel.trigger_typing()
with open("funfacts.json", "r") as f:
jokes = json.load(f)
ran = randrange(len(jokes["funfacts"]))
try:
await ctx.respond(f"Jednou jsem slyšela že: {jokes["funfacts"][ran]["txt"]}")
except:
await ctx.send(f"Jednou jsem slyšela že: {jokes["funfacts"][ran]["txt"]}")
msg = await ctx.send(f"Co myslíš?")
jokes["funfacts"][ran]["visited"] =+ 1
love = ""
await msg.add_reaction("\U0001F44D")
await msg.add_reaction("\U0001F44E")
await asyncio.sleep(300)
msg_cached = discord.utils.get(bot.cached_messages, id=msg.id)
for emo in msg_cached.reactions:
if emo.emoji == "👍":
jokes["funfacts"][ran]["upvotes"]+= emo.count - 1
elif emo.emoji == "👎":
jokes["funfacts"][ran]["downvotes"]+= emo.count - 1
elif emo.emoji == "🫀":
love = ', já Vás mám taky ráda 🫀.'
await msg.edit(content=f"Děkuju za ohodnoceni. {love}")
await msg.remove_reaction("\U0001F44D", msg.author)
await msg.remove_reaction("\U0001F44E", msg.author)
with open("funfacts.json", "w") as f:
json.dump(jokes, f)
return True
bot.run("MTMyNTk1Mzg4OTcyNzQxNDM0Mw.G2Tgo0.2OhbgaVYhys0BhxJMVgGDnG0W5qvxH-soJML3M")