-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFunctionality.py
157 lines (136 loc) · 8.17 KB
/
Functionality.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# For Discord
import discord
from discord.ext import commands
from discord.partial_emoji import PartialEmoji
# from discord_slash import cog_ext, SlashContext
# Api to interact with wikipedia
import wikipedia
# Api to interact with wolfram alpha
import wolframalpha
# Api to convert currency
from currency_converter import CurrencyConverter
# Api to beautify tables
from tabulate import tabulate
# Library to make request for url shortener
from urllib.request import urlopen
l = '''
██████╗ ██████╗ ██████╗ ██╗███████╗ ██████╗████████╗
██╔══██╗██╔══██╗██╔═══██╗ ██║██╔════╝██╔════╝╚══██╔══╝
██████╔╝██████╔╝██║ ██║ ██║█████╗ ██║ ██║
██╔═══╝ ██╔══██╗██║ ██║██ ██║██╔══╝ ██║ ██║
██║ ██║ ██║╚██████╔╝╚█████╔╝███████╗╚██████╗ ██║
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚════╝ ╚══════╝ ╚═════╝ ╚═╝
███╗ ███╗ █████╗ ██████╗ ██╗ ██╗
████╗ ████║██╔══██╗██╔══██╗██║ ██╔╝
██╔████╔██║███████║██████╔╝█████╔╝
██║╚██╔╝██║██╔══██║██╔══██╗██╔═██╗
██║ ╚═╝ ██║██║ ██║██║ ██║██║ ██╗
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝
'''
# Function to write into database
def write_db(db: dict) -> None:
with open("database", 'w+') as file:
file.write(str(db))
# Function to get database
def get_db() -> dict:
with open('database') as file:
return eval(file.read())
# Loading the config file
with open('config.json') as file:
config = eval(file.read())
class Functionality(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
client = wolframalpha.Client(config["wolframalpha_key"])
self.wolframclient = client
self.db = get_db()
# URL shortener
@commands.hybrid_command(name="tinify", pass_context=True)
async def _tinify(self, ctx: commands.Context, url: str):
"""Command to shorten a url using tiny url"""
apiurl = "http://tinyurl.com/api-create.php?url="
tinyurl = await self.bot.loop.run_in_executor(None, lambda: urlopen(apiurl + url).read().decode("utf-8"))
await ctx.send(tinyurl)
@commands.hybrid_command(aliases=['wikipedia'])
async def wiki(self, ctx: commands.Context, *, searchmsg: str):
'''Searches wikipedia and shows a summary of the search term'''
try:
async with ctx.typing():
url = wikipedia.page(searchmsg)
summary = wikipedia.summary(searchmsg, sentences=5)
# await ctx.send(summary+'\n\nUrl = `'+str(url)+'`')
embed = discord.Embed()
embed.title = url.title
embed.description = summary + \
'\n\n[Click here to read more]('+url.url+')'
if url.images != None:
embed.set_image(url=url.images[0])
await ctx.send(embed=embed)
except wikipedia.exceptions.PageError:
await ctx.send("Could not find a page with that search term")
except wikipedia.exceptions.DisambiguationError:
await ctx.send("Too many results. Please be more specific")
@commands.hybrid_command(aliases=['wolfram'])
async def math(self, ctx: commands.Context, *, searchquery: str):
'''Tries to solve a few basic math problems'''
try:
async with ctx.typing():
res = self.wolframclient.query(searchquery)
for i in res:
# if i['@title'] == 'Result':
# await ctx.send(i['subpod']['img']['@src'])
embed = discord.Embed()
if i['@title'] == 'Input interpretation' or i['@title'] == 'Input':
embed.title = 'Input Interpretation:'
embed.set_image(url=i['subpod']['img']['@src'])
await ctx.send(embed=embed)
elif i['@title'] == 'Result' or i['@title'] == 'Implicit plot' or i['@title'] == 'Plots' or i['@title'] == 'Surface plot' or i['@title'] == 'Volume of solid':
try:
embed.title = 'Result'
embed.set_image(url=i['subpod']['img']['@src'])
await ctx.send(embed=embed)
except:
embed.title = 'Result'
embed.set_image(url=i['subpod'][0]['img']['@src'])
await ctx.send(embed=embed)
except Exception as e:
await ctx.send('Something went wrong. (Math is in beta) '+str(e))
@commands.hybrid_command(name='currency_converter', aliases=['currconv'])
async def _currency_converter(self, ctx: commands.Context, value: int, frm: str, to: str):
'''Converts currency from one unit to another'''
c = CurrencyConverter()
try:
newvalue = c.convert(value, frm.upper(), to.upper())
await ctx.send(str(value)+' '+frm.upper()+' is equal to '+str(newvalue)+' '+to.upper())
except Exception as e:
await ctx.send("An Error occured: "+str(e))
@commands.hybrid_command(aliases=['l'])
async def logo(self, ctx: commands.Context):
'''Displays MarkBot's logo'''
await ctx.send('```'+l+'\nDeveloped by Wilford Warfstache#0256, started on April 16th, 2019'+'```')
await ctx.send(file=discord.File('logo.jpg'))
@commands.hybrid_command(aliases=['inv'])
async def invite(self, ctx: commands.Context):
'''Invite the bot to your server!'''
embedVar = discord.Embed(title="Invite MarkBot to your server!",
description="MarkBot is a project that is being worked on since April 19th, 2019. It was developed by [Anish R](https://github.com/z404). Feel free to fork the bot, and send pull requests if you've made any good changes. If you're interested in discussing future features to this bot, dm <@353835291053785088> to discuss it further", color=0x00ff00)
embedVar.add_field(
name="Invite Markbot", value='[Click here to invite MarkBot](https://discord.com/api/oauth2/authorize?client_id=781403770721402901&permissions=8&scope=bot%20applications.commands)\n', inline=False)
embedVar.add_field(name="Invite MarkBot Beta",
value="[Click here to invite MarkBot Beta](https://discord.com/api/oauth2/authorize?client_id=808973332988952586&permissions=8&scope=bot%20applications.commands)\nMarkBot Beta is an unstable release, and will not be online at all times, but will have experimental features that aren't present in MarkBot", inline=False)
embedVar.set_image(
url='https://t4.ftcdn.net/jpg/03/75/38/73/360_F_375387396_wSJM4Zm0kIRoG7Ej8rmkXot9gN69H4u4.jpg')
await ctx.send(embed=embedVar)
@commands.hybrid_command(name="stealemoji")
async def _stealemoji(self, ctx: commands.Context, emoji: discord.PartialEmoji):
"""If you have nitro, you can use this command to save an emoji of another server to the current server"""
img = await emoji.url.read()
name = emoji.name
emoji = await ctx.guild.create_custom_emoji(name=(name), image=img)
await ctx.send("Added "+str(emoji))
@_stealemoji.error
async def _emoji_error(self, ctx: commands.Context, error):
if "PartialEmoji" in str(error):
await ctx.send("Yo this emoji already exists!")
async def setup(bot):
await bot.add_cog(Functionality(bot))