Skip to content

Commit

Permalink
Better documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
s369269 committed Sep 1, 2021
1 parent 5d0d0ed commit 74f29cc
Show file tree
Hide file tree
Showing 14 changed files with 1,146 additions and 181 deletions.
58 changes: 51 additions & 7 deletions bot/commandmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from .decorators import isBotModCommand, isBotMod

class Commandmod(commands.Cog, name='Bot Mod Commands'):
"""You need privilage level 1 to use these commands."""
"""
You need privilage level 1 to use these commands.
"""
def __init__(self, bot, helpf, jh, xpf):
super(Commandmod, self).__init__()
self.bot = bot
Expand All @@ -24,6 +26,12 @@ def __init__(self, bot, helpf, jh, xpf):
@commands.command(name='textwl')
@isBotModCommand()
async def textwlCommandInterpretor(self, ctx, *inputs):
"""
param ctx: Discord Context object. Automatical passed.
param inputs: Tuple of arguments of commands.
Interpretes send commands beginning with user and calls the right function.
"""
lenght = len(inputs)
if lenght == 2 and inputs[0] == "add":
await self.addtextwhitelist(ctx, channelID = inputs[1])
Expand All @@ -41,12 +49,18 @@ async def textwlCommandInterpretor(self, ctx, *inputs):
await ctx.author.send(f"Command \"textwl {' '.join(inputs)}\" is not valid.")

async def addtextwhitelist(self, ctx, channelID = None):
"""
param ctx: Discord Context object.
param channelID: Integer or String of channels ID. Default is None.
Adds channel to whitlist so users can get XP in the channel.
"""
guilde = self.bot.get_guild(self.jh.getFromConfig("guilde"))
channels = self.helpf.getTextChannelsFrom(self.jh.getFromConfig("guilde"))
#
# When channelID is not given, use ctx.channel.id.
if not channelID:
channelID = ctx.channel.id
#Test if channel is in Server
# Test if channel is in Server
if str(channelID) in [str(channel.id) for channel in channels]:
#Try to write in whitelist
if self.jh.writeToWhitelist(channelID):
Expand All @@ -60,10 +74,16 @@ async def addtextwhitelist(self, ctx, channelID = None):
await ctx.send(message)

async def removetextwhitelist(self, ctx, channelID = None):
#
"""
param ctx: Discord Context object.
param channelID: Integer or String of channels ID. Default is None.
Removes channel from whitlist so users can not get XP in the channel.
"""
# When channelID is not given, use ctx.channel.id.
if not channelID:
channelID = ctx.channel.id
#Try to remove from whitelist
# Try to remove from whitelist
if self.jh.removeFromWhitelist(channelID):
channelName = str(self.bot.get_channel(int(channelID)))
message = f"Removed {channelName} with id {channelID} from Whitelist. This Text channel will not be logged."
Expand All @@ -83,6 +103,12 @@ async def removetextwhitelist(self, ctx, channelID = None):
@commands.command(name='voicebl')
@isBotModCommand()
async def voiceblCommandInterpretor(self, ctx, *inputs):
"""
param ctx: Discord Context object. Automatical passed.
param inputs: Tuple of arguments of commands.
Interpretes send commands beginning with user and calls the right function.
"""
lenght = len(inputs)
if lenght == 2 and inputs[0] == "add":
await self.addblacklist(ctx, inputs[1])
Expand All @@ -94,6 +120,12 @@ async def voiceblCommandInterpretor(self, ctx, *inputs):
await ctx.author.send(f"Command \"voicebl {' '.join(inputs)}\" is not valid.")

async def addblacklist(self, ctx, channelID):
"""
param ctx: Discord Context object.
param channelID: Integer or String of channels ID.
Adds channel to whitlist so users can not get XP in the channel.
"""
guilde = self.bot.get_guild(self.jh.getFromConfig("guilde"))
channels = self.helpf.getVoiceChannelsFrom(self.jh.getFromConfig("guilde"))
#Test if channel is in Server
Expand All @@ -110,8 +142,13 @@ async def addblacklist(self, ctx, channelID):
await ctx.send(message)

async def removeblacklist(self, ctx, channelID = None):
#Try to remove from Blacklist
#
"""
param ctx: Discord Context object.
param channelID: Integer or String of channels ID. Default is None.
Removes channel from whitlist so users can get XP in the channel.
"""
# When channelID is not given, use ctx.channel.id.
if not channelID:
channelID = ctx.channel.id
if self.jh.removeFromBalcklist(channelID):
Expand All @@ -133,6 +170,12 @@ async def removeblacklist(self, ctx, channelID = None):
@commands.command(name='dp', brief='Prints the Data of the Users', description='You need privilege level 1 to use this command. Prints the Username, userID, level, voiceXP, textXP and textCount off all users on the server.')
@isBotModCommand()
async def printData(self,ctx):
"""
param ctx: Discord Context object. Automatical passed.
Prints all user data in format:
User: "User name", UserID: UserID, Level: int, VoiceXP: int, TextXP: int, Messages: int.
"""
guilde = str(self.bot.get_guild(int(self.jh.getFromConfig("guilde"))))
message = f"Printing data of server {guilde}:\n"
# Sorts user by there usernames
Expand All @@ -147,6 +190,7 @@ async def printData(self,ctx):
#Handel not existing UserIDs
if user != None:
username = user.name
# Message format
messageadd = f"\nUser: {username}, UserID: {userID}, Level: {level}, VoiceXP: {voiceXP}, TextXP: {textXP}, Messages: {textCount}."
if len(message)+len(messageadd)>2000: #Get around 2000 char discord text limit
await ctx.send(message)
Expand Down
31 changes: 23 additions & 8 deletions bot/commandmodserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,31 @@
import asyncio

def hasAnyRole(*items):
"""
commands.has_any_role() does not work in DM since a users can't have roles.
This on pulls the roles from the conffigured guilde and makes the same check as commands.has_any_role().
"""
def predicate(ctx):
return Commandmodserver.helpf.hasOneRole(ctx.author.id, [*items])
return commands.check(predicate)
"""
Type: Decorator for functions with ctx object in args[1].
param items: Tuple of Strings and/or integers wit Discord Channel ids or names.
Check if a user has any of the roles in items.
Only use for commands, which don't use @commands.command
commands.has_any_role() does not work in DM since a users can't have roles.
This on pulls the roles from the configured guilde and makes the same check as commands.has_any_role().
Function is not in decorators.py since the Bot or Helpfunction Object is needed.
"""
def decorator(func):
def wrapper(*args, **kwargs):
if Commandpoll.helpf.hasOneRole(args[1].author.id, [*items]):
return func(*args, **kwargs)
return passFunc()
return wrapper
return decorator

class Commandmodserver(commands.Cog, name='Server Mod Commands'):
"""docstring for Commandmodserver"""
"""
Currently unused
"""

helpf = None

Expand Down
5 changes: 4 additions & 1 deletion bot/commandowner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import asyncio

class Commandowner(commands.Cog, name='Bot Owner Commands'):
"""You need privilage level 2 to use these commands."""
"""
You need privilage level 2 to use these commands.
Only for development and Bot Owner.
"""
def __init__(self, bot, helpf, tban, jh, xpf):
super(Commandowner, self).__init__()
self.bot = bot
Expand Down
109 changes: 90 additions & 19 deletions bot/commandpoll.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,31 @@
from .decorators import *

def hasAnyRole(*items):
"""
commands.has_any_role() does not work in DM since a users can't have roles.
This on pulls the roles from the configured guilde and makes the same check as commands.has_any_role().
"""
def decorator(func):
def wrapper(*args, **kwargs):
if Commandpoll.helpf.hasOneRole(args[1].author.id, [*items]):
return func(*args, **kwargs)
return passFunc()
return wrapper
return decorator
"""
Type: Decorator for functions with ctx object in args[1].
param items: Tuple of Strings and/or integers wit Discord Channel ids or names.
Check if a user has any of the roles in items.
Only use for commands, which don't use @commands.command
commands.has_any_role() does not work in DM since a users can't have roles.
This on pulls the roles from the configured guilde and makes the same check as commands.has_any_role().
Function is not in decorators.py since the Bot or Helpfunction Object is needed.
"""
def decorator(func):
def wrapper(*args, **kwargs):
if Commandpoll.helpf.hasOneRole(args[1].author.id, [*items]):
return func(*args, **kwargs)
return passFunc()
return wrapper
return decorator

class Commandpoll(commands.Cog, name='Poll Commands'):
"""These Commands are for creating polls."""
"""
These Commands define interactions with polls.
"""

helpf = None

Expand All @@ -31,6 +42,12 @@ def __init__(self, bot, helpf, poll, jh):

@commands.command(name='poll')
async def pollCommandInterpretor(self, ctx, *inputs):
"""
param ctx: Discord Context object. Automatical passed.
param inputs: Tuple of arguments of commands.
Interpretes send commands beginning with user and calls the right function.
"""
lenght = len(inputs)
if lenght == 2 and inputs[0] == "close":
await self.poll_close(ctx, inputs[1])
Expand Down Expand Up @@ -68,8 +85,15 @@ async def pollCommandInterpretor(self, ctx, *inputs):
@isDM()
@hasAnyRole("CEO","COO","chairman")
async def pollCreate(self, ctx, pollName):
#Creates a poll with the given name.
#Sends a overwiew of the poll.
"""
param ctx: Discord Context object.
param pollName: String.
Creates a poll with the given name.
Poll will have the lowest possible ID.
Sends a overwiew of the poll.
"""
message = ""
if len(pollName) <= 71:
pollID = self.poll.newPoll(pollName)
Expand All @@ -85,7 +109,13 @@ async def pollCreate(self, ctx, pollName):
@isDM()
@hasAnyRole("CEO","COO","chairman")
async def pollSend(self, ctx, pollID):
#Sends the poll with options to the user.
"""
param ctx: Discord Context object.
param pollID: Integer. ID of a poll in poll.json
Sends the poll with options to the user.
Does not change status. Only as a preview.
"""
message = ""
if self.poll.isAPollID(pollID):
message = self.poll.pollString(pollID)
Expand All @@ -96,7 +126,14 @@ async def pollSend(self, ctx, pollID):
@isDM()
@hasAnyRole("CEO","COO","chairman")
async def optionAdd(self, ctx, pollID, optionName):
#Trys to add a option to the poll.
"""
param ctx: Discord Context object.
param pollID: Integer. ID of a poll in poll.json
param optionName: String.
Trys to add a option to the poll with optionName.
New option gets lowest possible optionID.
"""
message = ""
if self.poll.isAPollID(pollID):
if len(optionName) <=112:
Expand All @@ -112,7 +149,14 @@ async def optionAdd(self, ctx, pollID, optionName):
@isDM()
@hasAnyRole("CEO","COO","chairman")
async def polloptionRemove(self, ctx, pollID, optionName):
#Trys to remove a option from a poll.
"""
param ctx: Discord Context object.
param pollID: Integer. ID of a poll in poll.json
param optionName: String.
Trys to remove a option from the poll with optionName.
All ids from options higher than the removed option will decremented.
"""
message = ""
if self.poll.isAPollID(pollID):
if not self.poll.optionRemove(pollID, str(optionName)):
Expand All @@ -125,7 +169,11 @@ async def polloptionRemove(self, ctx, pollID, optionName):
@isInChannelOrDM("🚮spam")
@hasAnyRole("CEO","COO","chairman","associate")
async def pollsList(self, ctx):
#Sends a list of all polls the the channel.
"""
param ctx: Discord Context object.
Sends the header of all polls in poll.json.
"""
message = ""
for pollID in self.poll.getAllPolls()[::-1]:
message += self.poll.pollHeader(pollID)
Expand All @@ -136,7 +184,12 @@ async def pollsList(self, ctx):
@isDM()
@hasAnyRole("CEO","COO","chairman")
async def poll_remove(self, ctx, pollID):
#Removes a poll.
"""
param ctx: Discord Context object.
param pollID: Integer. ID of a poll in poll.json
Removes a poll from poll.json if requirements are meet.
"""
message = ""
if self.poll.isAPollID(pollID):
pollName = self.poll.getName(pollID)
Expand All @@ -161,6 +214,12 @@ async def poll_remove(self, ctx, pollID):
@isNotInChannelOrDM("📂log","📢info","⏫level")
@hasAnyRole("CEO","COO","chairman")
async def poll_open(self, ctx, pollID):
"""
param ctx: Discord Context object.
param pollID: Integer. ID of a poll in poll.json
Posts a poll to channel, adds reactions to vote for options and opens poll.
"""
[messageID, channelID] = self.poll.getMessageID(pollID)
self.poll.pollOpen(pollID)
# Test if has a send poll string somewhere
Expand All @@ -187,6 +246,12 @@ async def poll_open(self, ctx, pollID):
@isDM()
@hasAnyRole("CEO","COO","chairman")
async def poll_close(self, ctx, pollID):
"""
param ctx: Discord Context object.
param pollID: Integer. ID of a poll in poll.json
Sets status of poll to closed and removes reactions from poll, so nobody can vote anymore.
"""
[messageID, channelID] = self.poll.getMessageID(pollID)
if self.poll.pollClose(pollID) and messageID and channelID:
channel = self.bot.get_channel(int(channelID))
Expand All @@ -202,6 +267,12 @@ async def poll_close(self, ctx, pollID):
@isNotInChannelOrDM("📂log","📢info","⏫level")
@hasAnyRole("CEO","COO","chairman")
async def poll_publish(self, ctx, pollID):
"""
param ctx: Discord Context object.
param pollID: Integer. ID of a poll in poll.json
Sets status of poll to published and removes reactions from poll, so nobody can vote anymore.
"""
if self.poll.pollPublish(pollID):
# Delet OPEN poll to resend as published
[messageID, channelID] = self.poll.getMessageID(pollID)
Expand Down
Loading

0 comments on commit 74f29cc

Please sign in to comment.