-
Notifications
You must be signed in to change notification settings - Fork 2
/
mvkdicebot.py
executable file
·152 lines (120 loc) · 4.06 KB
/
mvkdicebot.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
#!.venv/bin/python3
# MvKDiceBot: Discord bot for rolling dice for Mecha Vs Kaiju
# Copyright (C) 2023 Eric Eisenhart
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# https://github.com/freiheit/MvKDiceBot
"""MvKDiceBot: Discord bot for rolling dice for Mecha Vs Kaiju"""
import logging
import os
import warnings
from configparser import ConfigParser
import discord
from discord.ext import commands
import mvkroller
__version__ = "1.0.0"
DESCRIPTION = """A dice rolling bot for MvK
"""
intents = discord.Intents.default()
# intents.members = True
# intents.messages = True
intents.message_content = True # pylint: disable=assigning-non-slot
bot = commands.AutoShardedBot(
command_prefix=commands.when_mentioned_or("?"),
description=DESCRIPTION,
intents=intents,
)
@bot.event
async def on_ready():
"""Log when we start up"""
# pylint: disable=logging-fstring-interpolation
logger.warning(f"Logged in as {bot.user} (ID {bot.user.id})")
@bot.hybrid_command(
aliases=["r", "R", "roll", "rolldice", "diceroll"]
) # pylint: disable=no-member
async def mvkroll(ctx, *, dicestr: str):
"""Rolls NdN format pool of dice and does MvK rules math for you.
Example: '?roll 1d20 2d10 d8 2d6'
Add 'advantage' to discard lowest d20.
Add 'disadvantage' to discard highest d20.
Example: '?roll 2d20 2d10 advantage'
Example: '?roll 2d20 2d10 disadvantage'
Ignores anything extra it doesn't understand.
"""
try:
response = mvkroller.mvkroll(dicestr)
await ctx.reply(response)
except mvkroller.RollError as exc:
await ctx.reply(exc.getMessage())
raise
@bot.hybrid_command(
aliases=["p", "P", "pr", "PR", "justroll", "justdice", "plain"]
) # pylint: disable=no-member
async def plainroll(ctx, *, dicestr: str):
"""Just rolls NdN format pool of dice.
Example: '?roll 1d20 2d10 d8 2d6'
Ignores anything extra it doesn't understand.
"""
try:
response = mvkroller.plainroll(dicestr)
await ctx.reply(response)
except mvkroller.RollError as exc:
await ctx.reply(exc.getMessage())
raise
class ImproperlyConfigured(Exception):
"""Boring Exception Class"""
# pylint: disable=unnecessary-pass
pass
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
HOME_DIR = os.path.expanduser("~")
DEFAULT_CONFIG_PATHS = [
os.path.join(HOME_DIR, ".mvkdicebot.ini"),
os.path.join("/etc/mvkdicebot.ini"),
os.path.join(BASE_DIR, "mvkdicebot.ini"),
os.path.join("mvkdicebot.ini"),
]
def get_config():
"""Find and parse our config"""
# pylint: disable=redefined-outer-name
config = ConfigParser()
config_paths = []
for path in DEFAULT_CONFIG_PATHS:
if os.path.isfile(path):
config_paths.append(path)
break
else:
raise ImproperlyConfigured("No configuration file found.")
config.read(config_paths)
debug = config["MAIN"].getint("debug", 0)
if debug >= 3:
log_level = logging.DEBUG
elif debug >= 2:
log_level = logging.INFO
elif debug >= 1:
log_level = logging.WARNING
else:
log_level = logging.ERROR
logging.basicConfig(level=log_level)
logger = logging.getLogger(__name__)
logger.setLevel(log_level)
warnings.resetwarnings()
logger.addHandler(logging.StreamHandler())
return config, logger
config, logger = get_config()
bot.run(
token=config["MAIN"].get("authtoken"),
reconnect=True,
log_level=logging.WARNING,
)