-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnd.py
189 lines (170 loc) · 9.21 KB
/
dnd.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from time import sleep
import re
import discord
import os
from discord import reaction
from discord.ext import commands
import random
from pymongo.errors import DuplicateKeyError
import logging
import config
import asyncio
import pymongo
from pymongo import MongoClient
import config
import datetime
intents = discord.Intents.all()
intents.members = True
logging.basicConfig(level=logging.INFO)
client = commands.Bot(command_prefix='!', intents=intents)
dice_list = ["D4", "D6", "D8", "D10", "D12", "D20", "D100"]
# --------------------------
cluster = MongoClient(
config.database)
db = cluster["Dnd"]
characters = db["Characters"]
# -------------------------
@client.event
async def on_ready(): # When the bot is ready will post these things
print('Logged in as')
print(client.user.name)
# print(client.user.id)
print('DND Online')
print('------')
class Dnd(commands.Cog):
"""All DND Commands"""
@commands.command(name='roll', help='Rolls a dice')
async def roll(self, ctx, *, args):
"""Rolls a dice"""
if args.upper() in dice_list:
await ctx.send(f'You rolled a {args.upper()} and got {random.randint(1, int(args[1:]))}')
else:
await ctx.send('Please enter a valid dice')
await ctx.send('Valid dice are: D4, D6, D8, D10, D12, D20, D100')
# dnd character commands
@commands.command(name='create', help='Creates a new character')
async def create(self, ctx, *, args):
"""Creates a new character"""
if args is None: # if no args are given
await ctx.send('Please enter a name for your character')
await ctx.send('Example: !create character name')
else: # if args are given
if args.upper() == 'CHARACTER' or args.upper() == 'CHAR': # if args are character
await ctx.send('Please enter a name for your character')
await ctx.send('Example: !create character name')
sleep(1)
character_name = await client.wait_for('message', timeout=60.0)
sleep(1)
await ctx.send('Please enter a class for your character')
character_class = await client.wait_for('message', timeout=60.0)
await ctx.send(f'You created a new character named {character_name.content.capitalize()} '
f'and class is {character_class.content.capitalize()}')
try: # If the user has never had an entry in the database
characterload = {"_id": str(ctx.message.author.id),
"DiscordName": str(ctx.message.author.name),
"CharacterNum": int(1),
"CharacterName": str(character_name.content.capitalize()),
"CharacterClass": str(character_class.content.capitalize()),
# "CharacterLevel": int(1),
"CreatedDate": datetime.datetime.utcnow(),
"UpdatedDate": datetime.datetime.utcnow()}
characters.insert_one(characterload)
except: # If the user has had an entry in the database
await ctx.send('You already have a character'
'\nYou can only have 3 characters')
numofChars = characters.count_documents(
{"_id": {"$regex": str(ctx.message.author.id)}}) # Counts the number of characters
print(numofChars)
await ctx.send('You currently have ' + str(numofChars) + ' characters')
msg = await ctx.send('Would you like this new character to be saved?')
await msg.add_reaction('✅')
await msg.add_reaction('❌')
sleep(1)
create_another = await client.wait_for('message', timeout=60.0)
if create_another.content.upper() == 'YES':
await another_char(ctx, 'YES', character_name.content.capitalize(),
character_class.content.capitalize())
print('invoked')
else:
await ctx.send("You said No")
await ctx.send("Goodbye")
else: # If the user has entered a name for their character
await ctx.send('Please enter a class for your character')
@commands.command(name='delete', help='Deletes a character')
async def delete(self, ctx, *, args): # Deletes a character
"""Deletes a character"""
if args.upper() == 'CHARACTER' or args.upper() == 'CHAR':
await ctx.send('Please enter the FULL NAME of the character you want deleted')
await ctx.send('Or type the number of the character you want deleted')
await ctx.send('Example: !delete character 2')
sleep(1)
character_name = await client.wait_for('message', timeout=60.0)
if character_name.content == '1' or character_name.content == '2' or character_name.content == '3':
characters.delete_one({"CharacterNum": int(character_name.content)})
await ctx.send('Character has been deleted')
else:
try:
characters.delete_one({"CharacterName": character_name.content.capitalize()})
await ctx.send('Character deleted')
except:
await ctx.send('Character not found')
await ctx.send("use !list to see all your characters")
else:
await ctx.send('Please use !delete character')
@commands.command(name='list', help='Lists all characters')
async def list(self, ctx): # Lists all characters
"""Lists all characters"""
await ctx.send("Okay, let me look up your characters")
numofChars = characters.count_documents({"_id": {"$regex": str(ctx.message.author.id)}})
if numofChars == 0:
await ctx.send('You have no characters')
else:
await ctx.send('You currently have ' + str(numofChars) + ' characters')
for x in characters.find({"_id": {"$regex": str(ctx.message.author.id)}}):
await ctx.send("Character #" + str(x['CharacterNum']))
await ctx.send("Character Name: " + x['CharacterName'])
await ctx.send("Character Class: " + x['CharacterClass'])
await ctx.send("Created at: " + str(x['CreatedDate'])[0:10])
await ctx.send("---------------------------------")
async def another_char(ctx, args, character_name, character_class): # Creates another character 2nd or 3rd
# create_another = await client.wait_for('message', timeout=60.0)
if args.upper() == 'YES':
numofChars = characters.count_documents({"_id": {"$regex": str(ctx.message.author.id)}})
print(numofChars)
if numofChars == 3:
await ctx.send('You already have 3 characters')
await ctx.send('Would you like to delete a character?')
else:
await ctx.send(f'You created a new character named {character_name.capitalize()} '
f'and class is {character_class.capitalize()}')
try: # If the user has never had an entry in the database
characterload = {"_id": str(ctx.message.author.id) + "#" + str(numofChars),
"DiscordName": str(ctx.message.author.name),
"CharacterNum": int(int(numofChars) + 1),
"CharacterName": str(character_name.capitalize()),
"CharacterClass": str(character_class.capitalize()),
# "CharacterLevel": int(1),
"CreatedDate": datetime.datetime.utcnow(),
"UpdatedDate": datetime.datetime.utcnow()}
characters.insert_one(characterload)
except: # If the user has had an entry in the database
await ctx.send('You already have a character'
'\nYou can only have 3 characters')
await ctx.send('Would you like to create another character?')
create_another = await client.wait_for('message', timeout=60.0)
if create_another.content.upper() == 'YES':
await another_char(ctx, 'YES', character_name.capitalize(), character_class.capitalize())
else:
await ctx.send("You said No")
async def update_character(ctx, character_name, character_class):
characterload = {"_id": str(ctx.message.author.id),
"DiscordName": str(ctx.message.author.name),
"CharacterNum": int(1),
"CharacterName": str(character_name),
"CharacterClass": str(character_class),
# "CharacterLevel": int(1),
"CreatedDate": datetime.datetime.utcnow(),
"UpdatedDate": datetime.datetime.utcnow()}
characters.insert_one(characterload)
asyncio.run(client.add_cog(Dnd(client)))
client.run(config.token)