async def toggle_sound_timer(ctx, _userID): #Check if message is a dm if ((str(ctx.channel.type) == 'private') and (ctx.author.bot != True)): dm = ctx.author.dm_channel if dm is None: try: await ctx.author.create_dm() dm = ctx.author.dm_channel except Exception as e: cfg.Log('Error opening DM channel:' + e) #If command caller is an admin continue if (str(ctx.author.id) in cfg.adminUsers): #if user is in no time limit list, remove them if str(_userID) in cfg.noSoundTimer: cfg.noSoundTimer.remove(_userID) await dm.send('Enabled {}\'s sound timer'.format('<@!' + _userID + '>')) cfg.Log('{} Enabled {}\'s sound timer'.format( ctx.author.name, '<@!' + _userID + '>')) #if user not in the list, add them to it else: cfg.noSoundTimer.append(_userID) await dm.send('Disabled {}\'s sound timer'.format('<@!' + _userID + '>')) cfg.Log('{} Disabled {}\'s sound timer'.format( ctx.author.name, '<@!' + _userID + '>')) JSONreader.WriteToConfig(cfg.configPath, 'no_sound_timer', cfg.noSoundTimer) else: await dm.send('You do not have permissions to use this command!') cfg.Log('{} tried to use ToggleSoundTimer command'.format( ctx.author.name))
async def remove_admin(ctx, _userID): #Check if message is a dm if ((str(ctx.channel.type) == 'private') and (ctx.author.bot != True)): dm = ctx.author.dm_channel if dm is None: try: await ctx.author.create_dm() dm = ctx.author.dm_channel except Exception as e: cfg.Log('Error opening DM channel:' + e) #If the user who used the command is an admin and the userID he presented is not already in the adminUsers file if (str(ctx.message.author.id) in cfg.adminUsers) and (_userID in cfg.adminUsers): cfg.adminUsers.remove(_userID) #Write new admin to the config file JSONreader.WriteToConfig(cfg.configPath, "admin_user", cfg.adminUsers) cfg.Log('{} removed {} to admin list'.format(ctx.message.author.name, _userID)) await dm.send( '{} has been removed from the admin list.'.format(_userID)) else: #If the userID is already and admin alert the user if _userID not in cfg.adminUsers: await dm.send('{} is not an admin.'.format(_userID)) #Otherwise the user must not be an admin, as so alert them of this and log the attempt else: await dm.send('You do not have permissions to use this command!') cfg.Log('{} tried to remove {} as an admin but was denied access.'. format(ctx.message.author.name, _userID))
async def max_file_size(ctx, size): #Check if message is a dm if ((str(ctx.channel.type) == 'private') and (ctx.author.bot != True)): dm = ctx.author.dm_channel if dm is None: try: await ctx.author.create_dm() dm = ctx.author.dm_channel except Exception as e: cfg.Log('Error opening DM channel:' + e) #If invoker is an admin user and the file size is more than 0 if (str(ctx.author.id) in cfg.adminUsers) and (int(size) > 0): cfg.fileSizeAllowed = size JSONreader.WriteToConfig(cfg.configPath, 'max_file_size', cfg.fileSizeAllowed) await dm.send('Changed max file size to {} bytes'.format( cfg.fileSizeAllowed)) cfg.Log('{} changed the max file size to {} bytes'.format( ctx.author.name, cfg.fileSizeAllowed)) else: await dm.send('You do not have permissions to use this command!') cfg.Log('{} tried to use MaxFileSize command'.format(ctx.author.name))
async def intro_toggle(ctx): #Check if message is a dm if ((str(ctx.channel.type) == 'private') and (ctx.author.bot != True)): dm = ctx.author.dm_channel if dm is None: try: await ctx.author.create_dm() dm = ctx.author.dm_channel except Exception as e: cfg.Log('Error opening DM channel:' + e) #Get user ID user = str(ctx.author.id) #if user has disabled intro, enable it if user in cfg.disabledIntros: cfg.disabledIntros.remove(user) await dm.send('You have enabled your intro song(s).') cfg.Log(user + ' has enabled their intro sound.') #if user has an enabled sound, disable it else: cfg.disabledIntros.append(user) await dm.send('You have disabled your intro song(s).') cfg.Log(user + ' has disabled their intro sound.') #Write changes to config file JSONreader.WriteToConfig(cfg.configPath, 'user_disabled_intro', cfg.disabledIntros)
async def blocked_channels(ctx, channel=None): #Check if message is a dm if ((str(ctx.channel.type) == 'private') and (ctx.author.bot != True)): dm = ctx.author.dm_channel if dm is None: try: await ctx.author.create_dm() dm = ctx.author.dm_channel except Exception as e: cfg.Log('Error opening DM channel:' + e) if str(ctx.message.author.id) in cfg.adminUsers: #if channel name is passed to command if channel is not None: #if the channels in the list remove it if channel in cfg.blockedChannels: cfg.blockedChannels.remove(channel) JSONreader.WriteToConfig(cfg.configPath, 'blocked_channels', cfg.blockedChannels) await dm.send( 'Removed {} from blocked channels list'.format(channel)) cfg.Log('{} removed {} from blocked channels list'.format( ctx.message.author.name, channel)) #if the channel is not in the list add it else: cfg.blockedChannels.append(channel) JSONreader.WriteToConfig(cfg.configPath, 'blocked_channels', cfg.blockedChannels) await dm.send( 'Added {} to blocked channels list'.format(channel)) cfg.Log('{} added {} to blocked channels list'.format( ctx.message.author.name, channel)) #if no argument passed to command else: tmpString = '' for entry in cfg.blockedChannels: tmpString = tmpString + '{}\n'.format(entry) await dm.send('Blocked channels:\n\n{}'.format(tmpString)) else: await dm.send('You do not have permissions to use this command!') cfg.Log('{} tried to use BlockedChannels command'.format( ctx.author.name))
async def on_voice_state_update(member,before,after): #if the bot is not leaving a channel, is not currently playing a sound and the user is not a bot continue if ((after.channel is not None)and(len(bot.voice_clients)==0)and(member.bot==False)): #read variables from JSON in case of changes JSONreader.ReadConfig(cfg.configPath) #get user unique id user = str(member.id) #old way of making usernames easier to read, deprecated as a risk of users changing discriminator #user = member.name.lower()+'#'+member.discriminator #If moving to a NEW voice channel, the bot is allowed in the channel, and the user who triggered has not disabled their intro if ((before.channel!=after.channel)and(after.channel.name not in cfg.blockedChannels)and(user not in cfg.disabledIntros)): #if the user has a sounds folder with sound files in it if ((os.path.exists(cfg.soundsPath+'/'+user))and(len(os.listdir(cfg.soundsPath+'/'+user))>0)): currentChannel=after.channel #Wait to connect to users voice channel await currentChannel.connect() #find where the user is vcVal = 0 for i, vc in enumerate(bot.voice_clients): if vc.guild==member.guild: vcVal = i #check if the bot is already playing anything if not bot.voice_clients[vcVal].is_playing(): try: #choose a random sound from their sound folder to play files = os.listdir(cfg.soundsPath+'/'+user) song = random.choice(files) except Exception as e: cfg.Log('Failled to play {}\'s intro sound:{}'.format(member.name,e)) bot.voice_clients[vcVal].play(discord.FFmpegPCMAudio(cfg.soundsPath+'/'+user+'/'+song)) cfg.Log('Playing {}\'s intro sound: {}'.format(member.name,song)) #timer to allowe limited time for audio to play enabledTimer = True #if the user is found in the noSoundTimer list disabled their timer if user in cfg.noSoundTimer: enabledTimer = False startTime = time.time() while (bot.voice_clients[vcVal].is_playing() and (((int(time.time()-startTime)<=int(cfg.soundTime)) or (not enabledTimer)))): await asyncio.sleep(0.1) #Bot disconnects from voice channel await bot.voice_clients[vcVal].disconnect()
async def reload_config(ctx): #Check if message is a dm if ((str(ctx.channel.type) == 'private') and (ctx.author.bot != True)): dm = ctx.author.dm_channel if dm is None: try: await ctx.author.create_dm() dm = ctx.author.dm_channel except Exception as e: cfg.Log('Error opening DM channel:' + e) if (str(ctx.author.id) in cfg.adminUsers): JSONreader.ReadConfig(configPath) else: await dm.send('You do not have permissions to use this command!') cfg.Log('{} tried to use ReloadConfig command'.format(ctx.author.name))
async def max_files_allowed(ctx, size): #Check if message is a dm if ((str(ctx.channel.type) == 'private') and (ctx.author.bot != True)): dm = ctx.author.dm_channel if dm is None: try: await ctx.author.create_dm() dm = ctx.author.dm_channel except Exception as e: cfg.Log('Error opening DM channel:'+e) if (str(ctx.author.id) in cfg.adminUsers) and (int(size)>1): cfg.maxSoundFiles = size JSONreader.WriteToConfig(cfg.configPath,'max_sound_files',cfg.maxSoundFiles) await dm.send('Changed maximum amount of files to {}'.format(cfg.maxSoundFiles)) cfg.Log('{} changed the max amount of files to {}'.format(ctx.author.name,cfg.maxSoundFiles)) else: await dm.send('You do not have permissions to use this command!') cfg.Log('{} tried to use MaxFilesAllowed command'.format(ctx.author.name))
import cfg import JSONreader import sys import os import random #Discord.py specific import discord from discord.ext import commands from discord.voice_client import VoiceClient import asyncio import time #read in variables from config file JSONreader.ReadConfig(cfg.configPath) #setup bot to start bot = commands.Bot(command_prefix=cfg.cmdPrefix) cfg.Log('IAmFatAndLazy\'s StupidBoiV2 is launching...') ############################################################# #Command calls here #This keeps this file readable and easier to expand on #Called when the bot starts/connects to Discord @bot.event async def on_ready(): cfg.Log('Checking for sounds folder.')