Added additional commands. Removed unnecessary code in cmd_list commands. Added alias for cmd_roll command.
This commit is contained in:
parent
d90658d0c1
commit
5c226105d1
3 changed files with 50 additions and 23 deletions
|
|
@ -1,3 +1,5 @@
|
|||
# G4_Bot
|
||||
|
||||
Bot designed for exclusive use in the G4 Discord.
|
||||
|
||||
Lead Developer: Tanner Van Teeffelen
|
||||
|
|
@ -6,5 +6,6 @@
|
|||
"checkpoints": "cmd_checkpoints",
|
||||
"addcommand": "cmd_addcommand",
|
||||
"listcommands": "cmd_listcommands",
|
||||
"roll": "cmd_roll"
|
||||
"roll": "cmd_roll",
|
||||
"r": "cmd_roll"
|
||||
}
|
||||
66
methods.py
66
methods.py
|
|
@ -1,5 +1,6 @@
|
|||
#methods.py
|
||||
|
||||
#Imports.
|
||||
import os, json, discord, random, math
|
||||
from dotenv import load_dotenv
|
||||
|
||||
|
|
@ -20,80 +21,100 @@ async def cmd_goodbye(ctx):
|
|||
async def cmd_whatareyou(ctx):
|
||||
await ctx.send("An idiot sandwich.")
|
||||
|
||||
#Add points for a user.
|
||||
async def cmd_addpoints(ctx, member: discord.Member = None, number = None):
|
||||
points_data = loadpoints()
|
||||
member = member or ctx.author
|
||||
user_id = str(member.id)
|
||||
|
||||
points_data = loadpoints() #Load all point history into a variable.
|
||||
member = member or ctx.author #Set the target equal to the user specified OR the sender of the message.
|
||||
user_id = str(member.id) #Sets the user ID equal to the ID of the user specified.
|
||||
pointAmount = number or 1 #Makes the amount to add either the amount specified, or just 1.
|
||||
points_data[user_id] = points_data.get(user_id, 0) + int(pointAmount)
|
||||
loadpoints(points_data)
|
||||
points_data[user_id] = points_data.get(user_id, 0) + int(pointAmount) #Add the desired amount of points to the user.
|
||||
loadpoints(points_data) #Reload the points data.
|
||||
await ctx.send(f"{member.mention} now has {points_data[user_id]} points!")
|
||||
|
||||
#Checks the current points of a user.
|
||||
async def cmd_checkpoints(ctx, member: discord.Member = None):
|
||||
points_data = loadpoints()
|
||||
|
||||
points_data = loadpoints() #Load all point history into a variable.
|
||||
member = member or ctx.author # Checks either the member specified, or the sender of the message.
|
||||
user_id = str(member.id)
|
||||
points = points_data.get(user_id, 0)
|
||||
user_id = str(member.id) #Set the user ID equal to the ID of the user specified.
|
||||
points = points_data.get(user_id, 0) #Retrieve the points of the user ID.
|
||||
await ctx.send(f"{member.mention} has {points} points.")
|
||||
|
||||
#Add a custom command to customcommands.json.
|
||||
async def cmd_addcommand(ctx, command, *message):
|
||||
|
||||
response = (" ".join(message))
|
||||
commands_temp = loadcommands("commands.json")
|
||||
customcommands_temp = loadcommands("customcommands.json")
|
||||
response = (" ".join(message)) #Takes in the custom message.
|
||||
commands_temp = loadcommands("commands.json") #Load the built-in commands to a temp variable.
|
||||
customcommands_temp = loadcommands("customcommands.json") #Load the custom commands to a temp variable.
|
||||
|
||||
#Checks to see if the command already exists in commands.json.
|
||||
if command in commands_temp:
|
||||
await ctx.send(f"Command '{command}' already exists in base commands! Not overwriting.")
|
||||
return
|
||||
|
||||
#Checks to see if the command already exists in customcommands.json.
|
||||
if command in customcommands_temp:
|
||||
await ctx.send(f"Command '{command}' already exists in custom commands! Not overwriting.")
|
||||
return
|
||||
|
||||
#Takes the response, and adds it to the customcommands.json file.
|
||||
customcommands_temp[command] = response
|
||||
with open("customcommands.json", "w", encoding="utf-8") as file:
|
||||
json.dump(customcommands_temp, file, indent=4)
|
||||
|
||||
await ctx.send(f"Command '{command}' added successfully!")
|
||||
|
||||
#Lists all built-in and custom commands currently set up.
|
||||
async def cmd_listcommands(ctx):
|
||||
|
||||
try:
|
||||
#Loads both custom and built-in commands to temp variables.
|
||||
commands_dict = loadcommands("commands.json")
|
||||
customcommands_dict = loadcommands("customcommands.json")
|
||||
|
||||
if not commands_dict:
|
||||
await ctx.send("No commands found in the .JSON file.")
|
||||
|
||||
#Adds each set of commands into a list variable, then displays it.
|
||||
commands_list = "\n".join([f"{PREFIX}**{cmd}**" for cmd, resp in commands_dict.items()])
|
||||
customcommands_list = "\n".join([f"{PREFIX}**{cmd}**" for cmd, resp in customcommands_dict.items()])
|
||||
|
||||
await ctx.send(f"**Available Built-In Commands:**\n\n{commands_list}")
|
||||
|
||||
#Checks to see if there are any custom commands, and then lists them if there are any.
|
||||
with open ("customcommands.json", 'r') as file:
|
||||
data = json.load(file)
|
||||
if data != {}:
|
||||
customcommands_list = "\n".join([f"{PREFIX}**{cmd}**" for cmd, resp in customcommands_dict.items()])
|
||||
await ctx.send(f"\n\n**Available Custom Commands:**\n\n{customcommands_list}\n\n")
|
||||
|
||||
#Exception for failing to load commands. Typically indicates a missing commands.json file.
|
||||
except Exception as e:
|
||||
await ctx.send(f"Error loading commands: {e}")
|
||||
|
||||
#Rolls a dice roll. Supports modifiers as well.
|
||||
async def cmd_roll(ctx, inputValue, sign = None, addition = None):
|
||||
|
||||
#Initializing variables for the subtotal and total.
|
||||
subtotal = 0
|
||||
total = 0
|
||||
|
||||
#Rolls the dice.
|
||||
try:
|
||||
#Splits the input into variables separated by the letter 'd', for number of sides, and how many sides.
|
||||
number, sides = map(int, inputValue.lower().split('d'))
|
||||
|
||||
#For however many times you roll, it passes the number of sides to a rolldie function, and adds the result to subtotal.
|
||||
for _ in range(number):
|
||||
subtotal += rolldie(sides)
|
||||
|
||||
#Checks to see if any modifiers were applied. If so, apply the logic.
|
||||
if sign and addition:
|
||||
total = subtotal
|
||||
addition = int(addition)
|
||||
if sign == '+':
|
||||
addition = int(addition) #Explicitly converts the modifier value to an integer.
|
||||
if sign == '+': #Addition.
|
||||
total = subtotal + addition
|
||||
elif sign == '-':
|
||||
elif sign == '-': #Subtraction.
|
||||
total = subtotal - addition
|
||||
elif sign in ('*', 'X', 'x'):
|
||||
elif sign in ('*', 'X', 'x'): #Multiplication.
|
||||
total = subtotal * addition
|
||||
elif sign == '/':
|
||||
elif sign == '/': #Division, rounding down.
|
||||
total = math.floor(subtotal / addition)
|
||||
else:
|
||||
await ctx.send("Invalid mathematical sign passed.")
|
||||
|
|
@ -107,6 +128,7 @@ async def cmd_roll(ctx, inputValue, sign = None, addition = None):
|
|||
### INTERNAL METHODS - ALL METHODS MUST BE LOWERCASE ###
|
||||
########################################################
|
||||
|
||||
#Loads commands into variable.
|
||||
def loadcommands(filename):
|
||||
if not os.path.exists(filename):
|
||||
with open(filename, "w", encoding="utf-8") as file:
|
||||
|
|
@ -115,6 +137,7 @@ def loadcommands(filename):
|
|||
with open(filename, "r", encoding="utf-8") as file:
|
||||
return json.load(file)
|
||||
|
||||
#Loads points into variable.
|
||||
def loadpoints():
|
||||
if not os.path.exists("points.json"):
|
||||
with open("points.json", "w", encoding="utf-8") as file:
|
||||
|
|
@ -128,6 +151,7 @@ def savepoints(points_data):
|
|||
with open("points.json", "w", encoding="utf-8") as file:
|
||||
json.dump(points_data, file, indent=4)
|
||||
|
||||
#Rolls die for cmd_roll
|
||||
def rolldie(sides):
|
||||
return random.randint(1, sides)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue