Added notation support for rolling dice
This commit is contained in:
parent
d79ef7281c
commit
dd85ba49a5
5
bot.py
5
bot.py
|
@ -18,8 +18,7 @@ bot = commands.Bot(command_prefix=config.commandPrefix, description=config.botDe
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
# Change the status upon connection to the default status
|
# Change the status upon connection to the default status
|
||||||
game = discord.Game(name=config.defaultStatus, type=0)
|
await bot.change_status(discord.Game(name=config.defaultStatus, type=0))
|
||||||
await bot.change_status(game)
|
|
||||||
cursor = config.getCursor()
|
cursor = config.getCursor()
|
||||||
|
|
||||||
cursor.execute('use {0}'.format(config.db_default))
|
cursor.execute('use {0}'.format(config.db_default))
|
||||||
|
@ -29,7 +28,7 @@ async def on_ready():
|
||||||
destination = discord.utils.find(lambda m: m.id == result, bot.get_all_channels())
|
destination = discord.utils.find(lambda m: m.id == result, bot.get_all_channels())
|
||||||
await bot.send_message(destination, "I have just finished restarting!")
|
await bot.send_message(destination, "I have just finished restarting!")
|
||||||
cursor.execute('update restart_server set channel_id=0 where id=1')
|
cursor.execute('update restart_server set channel_id=0 where id=1')
|
||||||
config.closeConnection()
|
config.closeConnection()
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_message(message):
|
async def on_message(message):
|
||||||
|
|
36
cogs/core.py
36
cogs/core.py
|
@ -75,9 +75,7 @@ class Core:
|
||||||
|
|
||||||
if len(results) > 0:
|
if len(results) > 0:
|
||||||
index = random.randint(0, len(results) - 1)
|
index = random.randint(0, len(results) - 1)
|
||||||
randImageUrl = results[index].get('representations').get('full')[2:]
|
imageLink = 'http://' + results[index].get('representations').get('full')[2:].strip()
|
||||||
randImageUrl = 'http://' + randImageUrl
|
|
||||||
imageLink = randImageUrl.strip()
|
|
||||||
else:
|
else:
|
||||||
await self.bot.say("No results with that search term, {0}!".format(ctx.message.author.mention))
|
await self.bot.say("No results with that search term, {0}!".format(ctx.message.author.mention))
|
||||||
return
|
return
|
||||||
|
@ -90,11 +88,33 @@ class Core:
|
||||||
await self.bot.say(response)
|
await self.bot.say(response)
|
||||||
|
|
||||||
@commands.command(pass_context=True)
|
@commands.command(pass_context=True)
|
||||||
async def roll(self, ctx):
|
async def roll(self, ctx, notation: str = "d6"):
|
||||||
"""Rolls a six sided die"""
|
"""Rolls a die based on the notation given
|
||||||
num = random.randint(1, 6)
|
Format should be #d#"""
|
||||||
fmt = '{0.message.author.name} has rolled a die and got the number {1}!'
|
try:
|
||||||
await self.bot.say(fmt.format(ctx, num))
|
dice = re.search("(\d?)d(\d?)",notation).group(1)
|
||||||
|
num = re.search("(\d?)d(\d?)",notation).group(2)
|
||||||
|
except AttributeError:
|
||||||
|
await self.bot.say("Please provide the die notation in #d#!")
|
||||||
|
return
|
||||||
|
if dice == '':
|
||||||
|
dice = '1'
|
||||||
|
if int(dice) > 10:
|
||||||
|
await self.bot.say("I'm not rolling more than 10 dice, I have tiny hands")
|
||||||
|
return
|
||||||
|
if int(num) > 25:
|
||||||
|
await self.bot.say("What die has more than 25 sides? Please, calm down")
|
||||||
|
return
|
||||||
|
valueStr = str(random.randint(1,int(num)))
|
||||||
|
for i in range(0,int(dice)):
|
||||||
|
value = random.randint(1, int(num))
|
||||||
|
valueStr += ", {}".format(value)
|
||||||
|
|
||||||
|
if int(dice) == 1:
|
||||||
|
fmt = '{0.message.author.name} has rolled a die and got the number {2}!'
|
||||||
|
else:
|
||||||
|
fmt = '{0.message.author.name} has rolled {1} dice and got the numbers {2}!'
|
||||||
|
await self.bot.say(fmt.format(ctx, dice, valueStr))
|
||||||
|
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
|
|
Loading…
Reference in a new issue