From 5305fe018680727768a3bf282bfeb5e388ca5c58 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Sun, 6 Jun 2021 22:08:18 +0200 Subject: [PATCH] refactor of code --- LICENSE | 21 +++++ env.py.dist | 3 - help/help.md | 1 + help/help_gifbot.md | 1 + help/help_roll.md | 33 ++++++++ jeeves | 53 ------------ jeeves.py | 78 ++++++++++++++++++ jeevesbot/__init__.py | 0 jeevesbot/__pycache__/__init__.cpython-38.pyc | Bin 0 -> 150 bytes jeevesbot/__pycache__/bothelp.cpython-38.pyc | Bin 0 -> 578 bytes jeevesbot/__pycache__/env.cpython-38.pyc | Bin 0 -> 344 bytes .../__pycache__/functions.cpython-38.pyc | Bin 0 -> 845 bytes jeevesbot/bothelp.py | 20 +++++ jeevesbot/env.py.dist | 5 ++ jeevesbot/functions.py | 26 ++++++ requirements.txt | 9 ++ setup.py | 0 17 files changed, 194 insertions(+), 56 deletions(-) create mode 100644 LICENSE delete mode 100644 env.py.dist create mode 100644 help/help.md create mode 100644 help/help_gifbot.md create mode 100644 help/help_roll.md delete mode 100644 jeeves create mode 100644 jeeves.py create mode 100644 jeevesbot/__init__.py create mode 100644 jeevesbot/__pycache__/__init__.cpython-38.pyc create mode 100644 jeevesbot/__pycache__/bothelp.cpython-38.pyc create mode 100644 jeevesbot/__pycache__/env.cpython-38.pyc create mode 100644 jeevesbot/__pycache__/functions.cpython-38.pyc create mode 100644 jeevesbot/bothelp.py create mode 100644 jeevesbot/env.py.dist create mode 100644 jeevesbot/functions.py create mode 100644 requirements.txt create mode 100644 setup.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c1713d7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Peter van Arkel + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/env.py.dist b/env.py.dist deleted file mode 100644 index 2e8c8f8..0000000 --- a/env.py.dist +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env python3 - -TOKEN = 'discord-bot-token-here' \ No newline at end of file diff --git a/help/help.md b/help/help.md new file mode 100644 index 0000000..877e7d4 --- /dev/null +++ b/help/help.md @@ -0,0 +1 @@ +`General` \ No newline at end of file diff --git a/help/help_gifbot.md b/help/help_gifbot.md new file mode 100644 index 0000000..cd962f2 --- /dev/null +++ b/help/help_gifbot.md @@ -0,0 +1 @@ +`Test` \ No newline at end of file diff --git a/help/help_roll.md b/help/help_roll.md new file mode 100644 index 0000000..1821d99 --- /dev/null +++ b/help/help_roll.md @@ -0,0 +1,33 @@ +`!roll` +All forms of diceroll notations should work properly, including adding or detracting dice with each other, adding or detracting numbers from dicerolls, or even adding or detracting sums within brackets. Below are some examples. + +``` +Simple rolls + +!roll 1d20+6 +!roll 1d20-(2+6) +!roll 1d20-4+(1d4+3) + +Advanced rolls + +!roll 1d20x(N) +- exploding dice, will add extra dice on each roll above threshold N. If N is not defined, will default to maximum possible roll. + +!roll 6d6^(N) +- highest N dicerolls will be kept, so 6d6^2 will keep the highest two dice. + +!roll 6d6m(N) +- middle N dicerolls will be kept, so 6d6m2 will keep the middle two dice. + +!roll 6d6v(N) +- lowest N dicerolls will be kept, so 6d6l2 wil keep the lowest two dice. + +!roll 2d6r(N) +- will reroll any dice that are below threshold N. The reroll is possible to be below the threshold N. + +!roll 2d6rr(N) +- will reroll any dice that are below threshold N. The reroll will be at the very minimum threshold N. + +!roll 10d10s +- will sort the rolls in order, this will not change the result. +``` \ No newline at end of file diff --git a/jeeves b/jeeves deleted file mode 100644 index acfd63b..0000000 --- a/jeeves +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python3 - -import discord -from discord.ext import commands -import logging -import random -import urllib -from env import TOKEN - -# setup discord.py -client = discord.Client() -e = discord.Embed() -# setup logging -logger = logging.getLogger('jeeves') -logger.setLevel(logging.DEBUG) -handler = logging.FileHandler(filename='jeeves.log', encoding='utf-8', mode='w') -handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) -logger.addHandler(handler) - -def resolve(url): - return urllib.request.urlopen(url).url - -@client.event -async def on_message(message): - # we do not want the bot to reply to itself - if message.author == client.user: - return - if message.content.startswith('https://tenor.com/'): - crew = discord.Role.members("crew") - print(crew) - print(message.author) - #embed_url = message.content - #follow_url = embed_url + '.gif' - #full_url = resolve(follow_url) - #gif_url = full_url.split('?')[0] - #print(gif_url) - #embed = e.set_image(url=gif_url) - #await message.channel.send(embed=embed) - if message.content.startswith('https://giphy.com/'): - embed_url = message.content - image_code = embed_url.split('-')[-1] - gif_url = 'https://media.giphy.com/media/' + image_code + '/giphy.gif' - embed = e.set_image(url=gif_url) - await message.channel.send(embed=embed) - -@client.event -async def on_ready(): - print('### Active with id %s as %s ###' % (client.user.id,client.user.name) ) - # activity = discord.Activity(name='the threads of fate (pm !help for help)', type=discord.ActivityType.watching) - # await client.change_presence(activity=activity) - -if __name__ == '__main__': - client.run(TOKEN) \ No newline at end of file diff --git a/jeeves.py b/jeeves.py new file mode 100644 index 0000000..67e2cd8 --- /dev/null +++ b/jeeves.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 + +from types import resolve_bases +import discord +from discord.ext import commands +import logging +from jeevesbot import bothelp, functions, env + +# setup logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger('jeeves') +logger.setLevel(logging.INFO) +handler = logging.FileHandler(filename='jeeves.log', encoding='utf-8', mode='a') +handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) +logger.addHandler(handler) + +# setup discord.py +client = discord.Client() +e = discord.Embed() + +@client.event +async def on_message(message): + # we do not want the bot to reply to itself + if message.author == client.user: + return + if message.content.startswith('!help'): + parameters = message.content.split(' ', 1) + if len(parameters) == 2: + msg = format(bothelp.help(parameters[1])) + else: + msg = format(bothelp.help()) + await message.author.send(msg) + # giphy and tenor both have different structures to their links + if message.content.startswith('https://tenor.com/'): + roles = functions.checkrole(message.author.roles) + channel = functions.checkchannel(message.channel.id) + embed_url = message.content + follow_url = embed_url + '.gif' + full_url = functions.resolve(follow_url) + gif_url = full_url.split('?')[0] + embed = e.set_image(url=gif_url) + if channel is True: + if roles is not True: + await message.channel.send(embed=embed) + logger.info(message.author) + logger.info(gif_url) + if message.content.startswith('https://giphy.com/'): + roles = functions.checkrole(message.author.roles) + channel = functions.checkchannel(message.channel.id) + embed_url = message.content + image_code = embed_url.split('-')[-1] + gif_url = 'https://media.giphy.com/media/' + image_code + '/giphy.gif' + embed = e.set_image(url=gif_url) + if channel is True: + if roles is not True: + await message.channel.send(embed=embed) + logger.info(message.author) + logger.info(gif_url) + if message.content.startswith('!roll'): + parameters = message.content.split(' ', 1) + if len(parameters) == 2: + param = parameters[1] + roll,result = functions.roll(param) + msg = 'Rolling %s for {0.author.mention}: `%s`'.format(message) % (param,roll) + await message.channel.send(msg) + +@client.event +async def on_ready(): + print('### Active with id %s as %s ###' % (client.user.id,client.user.name) ) + activity = discord.Activity(name='Listening to !help', type=discord.ActivityType.listening) + +if __name__ == '__main__': + client.run(env.TOKEN) + +## TODO +# /tenor andere syntax +# tenor android keyboard andere syntax (https://c.tenor.com/) +# giphy verdubbelt url \ No newline at end of file diff --git a/jeevesbot/__init__.py b/jeevesbot/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/jeevesbot/__pycache__/__init__.cpython-38.pyc b/jeevesbot/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..811ae832891a627aadbd9189e57ce295bb88c18b GIT binary patch literal 150 zcmWIL<>g`kf}Sfo5v3^QsadLi9if&eFY8j9VrQ+i=^D;}~8S%5T5<9W1W&fft1i7w=Rf9PY@CkWtxcQ6e(Q1bC>0OUTbzkl#{PIo`5Gv zN?w6S*_JA=kSepsfr7+X^UcgR|?q4t%n29l% z%t)iYiLo&H6smlj&+PQVg$Rw{p+BZMNpF8(_km#me;B}swOENk+=@F`$w8hnZ{$m$ zKFC`&z@1nz6ZAZ%GsE=(7<~XM6|zCCg@6EqGTB=GyW@X*P0s3emTx}VHXxN#N97DV zq)A&|V%wKo`yLxRwM{w4n`=jA(>Pl0rhc8^XkqSIIxI(jsq>{`HVQha@~B;pUDSjV z6O|7Xv({ZB#bUeA$E+vYg-XYrEfw|Dwit`XbpgBJV_{r#wczvz&^~|b;kdqZ3#|JI z&9SWsd*^L%WYO2g`i6DB{(|_G%in>|zGmO(?Qfz^NjiD|DoK}G6#OI{vF~o9mWie3 lJaJM9N;g+YDmZMojglwuj2p3dyKv^J#dm4i^X~$=d<5(WkpBPx literal 0 HcmV?d00001 diff --git a/jeevesbot/__pycache__/env.cpython-38.pyc b/jeevesbot/__pycache__/env.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..bcd7c56dd1d040acc35ad2294eb488e46c707b66 GIT binary patch literal 344 zcmWIL<>g`k0;##X6Id7-7#@Q-$bc2daRB0CDIk%;5XF$f7{!>vl){|ClERw8md+Z* zl)@g&pvh5X?eF4h>X+qP>F1Je=$jR4>hI!c;8zuDpcm;{kR0Hsms=KO98i&6RcPiC zV(1i{n_T4O>zD0ZTxA@1i#IVPH#0A@xTGktB)=$!1!$Qh14lugjBzlSBf`)j_c+`( z49u}-Xo+=+&k6u@7#TQx_Zu<=f;pND4IDz1vjY4ynQyU%_E`U==;!C^6TFh42;?^~@yk*_BR@A)zaX(FJ2gkYD77HJI1}iO%+zB2l+5Df t{Gt@ytkl#pAQwU<<(KHE=9TFcRNmsS$<0qG%}KRm289yH;Vg_Si~!JCSP%dJ literal 0 HcmV?d00001 diff --git a/jeevesbot/__pycache__/functions.cpython-38.pyc b/jeevesbot/__pycache__/functions.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e460fa562d3da55615e064563f510a1dd62b0000 GIT binary patch literal 845 zcmZ9K&2AGh5P&`QX0!Qeq=JMHM{aw7eE|eiX$zIgB9U@g2|>Dc)lD|*)OI2vr6+g* z9-$oj47`c2oOl2(oS1QmC`upS87sVO-O~&1YKK2^7B_WxF zn$4g9pfj&|!GjiskU~b^SrN&Wj8V5_TPCPu*^yn;ZOKP0?ft5%YqRJq za1U|Y0Sd<^aKl&ZCrreM-9ny5K37KD=cnLG|xd zcByA7n~%*@)tOOqZ7ZiurEDfETWTYpO_W-qxmM@eWfzO4bd_%G^Z8e*Vs*V#l+98j z7#Ql-4mUAxxu2ln*bNZ%H8{S4Z*Yxr#V_#l4aKFxN3trFkBqKsFRI3+qDUI;##Gq! zF~-MOc~QIY9c$4+cetZ_8>3**g#?0c2Z%toKu^(Fu|xLhG2$@7aWLUiZX%o@jM!Bq z8P0|0XUHY2GlrfQ-Gjq-C;8_O?@y0X?psKzYyejGkbI}SROOTc_VM8cEP(--2Wanr z(S#}ckOCi}ScMr+V>2KW`auGW$j^>bz6R;NIXND_J;?K;(@_cqRvR~ssw;V)qaQdn z*OYwQqllY_Xs3zqZ08)3n+V;eM}nprIvJ=#{G9?ce?tTC$%~nmi(0*;23s0MA7_w= JBuZLI>px>irX&CW literal 0 HcmV?d00001 diff --git a/jeevesbot/bothelp.py b/jeevesbot/bothelp.py new file mode 100644 index 0000000..5a347e0 --- /dev/null +++ b/jeevesbot/bothelp.py @@ -0,0 +1,20 @@ +keywords = ['gifbot'] + +def help(keyword=''): + """Filter for general help or specific keyword help.""" + if not keyword: + file = open('help/help.md') + contents = file.read() + return (contents) + if keyword[0]: + keyword = str.lower(keyword) + if keyword in keywords: + document = 'help/help_' + keyword + '.md' + print(document) + file = open(document, 'r') + contents = file.read() + return (contents) + else: + file = open('help/help.md') + contents = file.read() + return (contents) \ No newline at end of file diff --git a/jeevesbot/env.py.dist b/jeevesbot/env.py.dist new file mode 100644 index 0000000..fb1ad44 --- /dev/null +++ b/jeevesbot/env.py.dist @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 + +TOKEN = 'discord-bot-token-here' +ADMIN_ROLE = 'role-to-exclude-from-gifbot' +GIFCHANNELS = 'add-channel-ids-for-bot-to-work-in' \ No newline at end of file diff --git a/jeevesbot/functions.py b/jeevesbot/functions.py new file mode 100644 index 0000000..b4e639a --- /dev/null +++ b/jeevesbot/functions.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python 3 + +from jeevesbot import env +import urllib +import dice + +# This function is necessary for tenor, because they do not allow linking directly to the gif and need resolving. +def resolve(url): + return urllib.request.urlopen(url).url + +# use the dice module for rolling +def roll(notation): + roll = dice.roll(notation) + result = int(roll) + return roll,result + +def checkrole(roles): + for role in roles: + if str(role) == env.ADMIN_ROLE: + return True + +def checkchannel(channelid): + if channelid in env.GIFCHANNELS: + return True + else: + return False diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0a5b11f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +aiohttp 3.7.4.post0 +async-timeout 3.0.1 +dice 3.1.2 +discord.py 1.7.1 +docopt 0.6.2 +multidict 5.1.0 +pyparsing 2.4.7 +typing-extensions 3.7.4.3 +yarl 1.6.3 \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..e69de29