refactor of code

This commit is contained in:
2021-06-06 22:08:18 +02:00
parent b3246e5e5d
commit 5305fe0186
17 changed files with 194 additions and 56 deletions

21
LICENSE Normal file
View File

@@ -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.

View File

@@ -1,3 +0,0 @@
#!/usr/bin/env python3
TOKEN = 'discord-bot-token-here'

1
help/help.md Normal file
View File

@@ -0,0 +1 @@
`General`

1
help/help_gifbot.md Normal file
View File

@@ -0,0 +1 @@
`Test`

33
help/help_roll.md Normal file
View File

@@ -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.
```

53
jeeves
View File

@@ -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)

78
jeeves.py Normal file
View File

@@ -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

0
jeevesbot/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

20
jeevesbot/bothelp.py Normal file
View File

@@ -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)

5
jeevesbot/env.py.dist Normal file
View File

@@ -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'

26
jeevesbot/functions.py Normal file
View File

@@ -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

9
requirements.txt Normal file
View File

@@ -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

0
setup.py Normal file
View File