/
Rpgbot.py
140 lines (130 loc) · 6.04 KB
/
Rpgbot.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
#!/usr/local/bin/python3
###############################################################################
#! This program is free software: you can redistribute it and/or modify
#! it under the terms of the GNU 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 General Public License for more details.
#! You should have received a copy of the GNU General Public License
#! along with this program. If not, see <http://www.gnu.org/licenses/>.
#! Copyright Cody Ferber, 2019.
###############################################################################
from contextlib import closing
from discord.ext import commands
from random import *
import asyncio
import discord
class Rpgbot(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.level = {}
self.xp = {}
self.get_next_level = {}
self.get_next_xp = {}
self.get_xp_remain = {}
self.next_level = [[2, 5],
[3, 10],
[4, 15],
[5, 20],
[6, 25],
[7, 30],
[8, 35],
[9, 40],
[10, 45],
[11, 50],
[12, 55],
[13, 60],
[15, 65],
[16, 70],
[17, 75],
[18, 80],
[19, 85],
[20, 90],
[21, 95],
[22, 100],
[23, 105],
[24, 110],
[25, 115]]
###############################################################################
@commands.command(name='attack', brief='Attack a monster.',
description='Attack a monster.')
@commands.cooldown(1, 2, commands.BucketType.channel)
async def attack(self, ctx):
author_id = ctx.message.author.id
get_next_level = self.next_level[self.level[author_id] - 1][0]
get_next_xp = self.next_level[get_next_level - 2][1]
get_xp_remain = get_next_xp - self.xp[author_id]
number = randint(0, 100)
if (number >= 40):
await ctx.send('You have defeated the monster!')
if (get_xp_remain >= 1):
get_xp_remain = get_xp_remain - 1
self.xp[author_id] = self.xp[author_id] + 1
await ctx.send('You gain 1 XP!')
if (get_xp_remain == 0):
self.level[author_id] = self.level[author_id] + 1
await ctx.send('You have gained a level!')
await ctx.send('Welcome to level {}!'
.format(self.level[author_id]))
elif (number >= 20) and (number <= 39):
await ctx.send('You poop your pants and flee!')
else:
await ctx.send('You die bravely in battle!')
if (get_xp_remain < 5):
await ctx.send('You lose 1 XP!')
self.xp[author_id] -= 1
else:
await ctx.send('You lose 0 XP!')
###############################################################################
@commands.command(name='charinfo', brief='Display character info.',
description='Display character info.')
@commands.cooldown(1, 5, commands.BucketType.channel)
async def charinfo(self, ctx):
author_id = ctx.message.author.id
get_next_level = self.next_level[self.level[author_id] - 1][0]
get_next_xp = self.next_level[get_next_level - 2][1]
get_xp_remain = get_next_xp - self.xp[author_id]
self.get_next_level[author_id] = get_next_level
self.get_next_xp[author_id] = get_next_xp
self.get_xp_remain[author_id] = get_xp_remain
await ctx.send('Level {}.'.format(self.level[author_id]))
await ctx.send('{} XP.'.format(self.xp[author_id]))
await ctx.send('You will gain a level in {} XP.'.format(get_xp_remain))
###############################################################################
@commands.command(name='register', brief='Register account with rpgbot.',
description='Register account with rpgbot.')
@commands.cooldown(1, 5, commands.BucketType.channel)
async def register(self, ctx):
author_id = ctx.message.author.id
self.level[author_id] = 1
self.xp[author_id] = 0
await ctx.send('User ID is now registered!')
await ctx.send('Use !charinfo for character stats!')
###############################################################################
@commands.command(name='roll', brief='Roll magical dice.',
description='Roll magical dice.')
@commands.cooldown(10, 30, commands.BucketType.channel)
async def roll(self, ctx, *args):
x = 0
await ctx.send('A magic die is rolled by {}.'.format(
ctx.message.author.name))
for num in args:
if num:
x += 1
if x == 1:
await ctx.send('It could have been any number'
' between 0 and {}, but this time it turned up a {}.'
.format(args[0], str(randint(0, int(args[0])))))
elif x == 2:
await ctx.send('It could have been any number'
' between {} and {}, but this time it turned up a {}.'
.format(args[0], args[1], str(randint(int(args[0]),
int(args[1])))))
else:
await ctx.send('Invalid number of inputs!')
###############################################################################
def setup(bot):
bot.add_cog(Rpgbot(bot))