async def on_ready(): await client.change_presence(activity=discord.Activity( type=discord.ActivityType.playing, name="Version c3.2.3")) #set the bot's playing status dev.log( s, f"logged in as {client.user.name}#{client.user.discriminator} with latency {round(client.latency*1000)}ms. THIS IS A CANARY BUILD!" ) dev.log( s, f"bot running in {__file__}") #get the directory the bot is running in postfact.start()
async def on_message(self, ctx): dev.log(s, f"message received: {ctx.content}") chatlog = open("chatlog.txt", "a") dev.log(s, "open chatlog.txt") chatlog.write( f"{str(datetime.now())} [{str(ctx.author)}][{str(ctx.channel)}] >>> {str(ctx.content)}\n" ) dev.log( s, f" write {datetime.now()} [{ctx.author}][{ctx.channel}] >>> {ctx.content} \\n" ) chatlog.close() dev.log(s, f"close chatlog.txt") return
async def unban(self, ctx, *, member, reason="unspecified"): dev.log(s, f"unban command called\ncheck for ban members permission") dev.log( s, f"unban members permission true\ngot all required arguments\n begin unban command execution" ) ban_list = await ctx.guild.bans() dev.log(s, f"get ban list in guild") member_name, member_discriminator = member.split("#") dev.log(s, f"target is {member_name}#{member_discriminator}") for ban in ban_list: user = ban.user dev.log(s, f"get ban {user}") if (user.name, user.discriminator) == (member_name, member_discriminator): dev.log(s, f"hit\nunban{member_name}#{member_discriminator}") unbanembed = discord.Embed( title="Member Unbanned", description= f"{member} was unbanned from the server by {ctx.author.mention}. Reason: `{reason}`.", color=discord.Color.green()) dev.log(s, f"create unbanembed") await ctx.send(embed=unbanembed) dev.log(s, f"send unbanembed") dev.log(s, f"end unban command execution") return
async def clearchat(self, ctx, amount): dev.log(s, f"clearchat command called\ncheck for ban members permission") dev.log( s, f"ban members permission true\ngot all required arguments\n begin ban command execution" ) await ctx.channel.purge(limit=int(amount) + 1) dev.log(s, f"purge {ctx.channel} with limit {amount}") clearchatembed = discord.Embed( title="Chat Cleared", description= f"The last `{amount}` messages were removed from the channel by {ctx.author.mention}.", color=discord.Color.green()) dev.log(s, f"create clearchatembed") await ctx.send(embed=clearchatembed) dev.log(s, f"send clearchatembed") dev.log(s, f"end clearchat command execution")
async def hist(self, ctx, user): dev.log(s, f"hist command called\ncheck for ban members permission") dev.log( s, f"hist members permission true\ngot all required arguments\n begin hist command execution" )
async def ban(self, ctx, member: discord.Member, *, reason="unspecified"): dev.log(s, f"ban command called\ncheck for ban members permission") dev.log( s, f"ban members permission true\ngot all required arguments\n begin ban command execution" ) banembed = discord.Embed( title="Member Banned", description= f"{member.mention} was banned from the server by {ctx.author.mention}. Reason: `{reason}`.", color=discord.Color.dark_red()) dev.log(s, f"create banembed") await ctx.send(embed=banembed) dev.log(s, f"send embed to chat") dm = await member.create_dm() dev.log(s, f"create dm with {member}") dmbanembed = discord.Embed( title=f"You have been banned!", description= f"You have been banned from `{ctx.guild.name}` by `{ctx.author}`. Reason: `{reason}`. If you feel the ban was unjustified, or you deserve a second chance, join this server and submit a ban appeal. https://discord.gg/N86JzKM", color=discord.Color.red()) dev.log(s, f"create banenbeddm") await dm.send(embed=dmbanembed) dev.log(s, f"send banembeddm to dm") await member.ban(reason=reason) dev.log(s, f"ban {member} for {reason}") dev.log(s, f"end ban command execution")
async def kick(self, ctx, member: discord.Member, *, reason="unspecified"): dev.log(s, f"kick command called\ncheck for kick members permission") dev.log( s, f"kick members permission true\ngot all required arguments\n begin kick command execution" ) kickembed = discord.Embed( title="Member Kicked", description= f"{member.mention} was kicked from the server by {ctx.author.mention}. Reason: `{reason}`.", color=discord.Color.red()) dev.log(s, f"create kickembed") await ctx.send(embed=kickembed) dev.log(s, f"send kickembed") dm = await member.create_dm() dev.log(s, f"create dm with {member}") dmkickembed = discord.Embed( title=f"You have been kicked!", description= f"You have been kicked from `{ctx.guild.name}` by `{ctx.author}`. Reason: `{reason}`.", color=discord.Color.red()) dev.log(s, f"create dmkickembed") await dm.send(embed=dmkickembed) dev.log(s, f"send dmkickembed to dm") await member.kick(reason=reason) dev.log(s, f"kick {member} for {reason}") dev.log(s, f"end kick command execution")
async def on_message(self, ctx): if ctx.channel.id == 703460208940417064: #canary counting channel pa = open("author.txt", "r") dev.log(s, f"open author.txt in read only mode") prev_author = str(pa.read()) pa.close() dev.log( s, f"prev author is {prev_author} and current author is {ctx.author}" ) if prev_author == str(ctx.author): dev.log(s, f"prev author matches current author") dev.log(s, f"deleting message and return") await ctx.delete() return current_author = str(ctx.author) dev.log(s, f"current author is {current_author}") pa = open("author.txt", "w") dev.log(s, f"open author.txt in write mode") pa.write(current_author) pa.close() dev.log(s, f"closing author.txt") dev.log(s, f"open counting.txt in read only mode") numfile = open("counting.txt", "r") prevnum = int(numfile.read()) dev.log(s, f"read counting.txt prevnum is {prevnum}") numfile.close() nextnum = prevnum + 1 numfilenew = open("counting.txt", "w+") dev.log(s, f"next number will be {nextnum}") if ctx.content == str(nextnum): dev.log(s, f"hit {ctx.content}") numfilenew.write(str(nextnum)) dev.log(s, f"write {nextnum} to counting.txt") dev.log(s, f"closing counting.txt and return") nextnum = int(nextnum) await ctx.channel.edit( topic=f"The next number will be {nextnum+1}") dev.log( s, f"new contents of counting.txt are {numfilenew.read()}") numfile.close() return else: dev.log(s, f"miss {ctx.content}") numfilenew.write(str(prevnum)) dev.log(s, f"write {prevnum} to counting.txt") await ctx.delete() pa = open("author.txt", "w") pa.close() dev.log(s, f"deleting message closing counting.txt and return") numfile.close() return
async def on_message_edit(self, before, after): dev.log(s, f"message was edited from '{before}' to '{after}'") if after.channel.id == 703460208940417064: #counting channel dev.log(s, f"message is in counting channel") await after.delete() #delete the message dev.log(s, f"deleted the message") messageeditembeddm = discord.Embed( title="You are not allowed to edit messages!", description= "You cannot edit messages in this channel! Please refrain from doing this as it makes it difficult to count.", colour=discord.Color.red(), ) dev.log(s, f"create messageeditembeddm") dm = await after.author.create_dm() dev.log(s, f"create a dm with {after.author}") await dm.send(embed=messageeditembeddm) dev.log(s, f"send embed to dm and return") return else: dev.log(s, f"message is in a different channel, return") return None #message is in a different channel
async def announcedm(self, ctx, *, announcement: str): dev.log(s, f"chatlog command called\ncheck for admin permission") dev.log( s, f"admin permission true\ngot all required arguments\n begin chatlog command execution" ) dmallembed1 = discord.Embed( title="AnnounceDM Started", description= "I have started sending out DMs. This might take some time depending on the number of members in the server.", color=discord.Color.green()) dev.log(s, f"create dmallembed1") dmallembed2 = discord.Embed( title="AnnounceDM Successful", description= f"Successfully sent the announcement to all server members.", color=discord.Color.green()) dev.log(s, f"create dmallembed2") await ctx.channel.send(embed=dmallembed1) for member in ctx.guild.members: announcementdm = await member.create_dm() dev.log(s, f"create dm with {member}") announcementdmembed = discord.Embed( title=f"**{ctx.guild.name} Announcement**", description=f"{announcement}", color=discord.Color.green()) dev.log(s, f"create announcementdmembed") await announcementdm.send(embed=announcementdmembed) dev.log(s, f"create send announcementdmembed to dm") await ctx.channel.send(embed=dmallembed2) dev.log(s, f"send dmallembed2 to {ctx.channel}")
async def chatlog(self, ctx): dev.log(s, f"chatlog command called\ncheck for admin permission") dev.log( s, f"admin permission true\ngot all required arguments\n begin chatlog command execution" ) dm = await ctx.author.create_dm() dev.log(s, f"create dm with {ctx.author}") filesentembeddm = discord.Embed( title="Here you go!", description="Here's the chat log file you requested.", color=discord.Color.green()) dev.log(s, f"create filesentembeddm") await dm.send(embed=filesentembeddm) dev.log(s, f"send embed to dm") await dm.send(file=discord.File('chatlog.txt')) dev.log(s, f"upload file chatlog.txt") filesentembed = discord.Embed( title="Log File Ready!", description="I have sent you a DM containing `chatlog.txt`", color=discord.Color.green()) dev.log(s, f"create filesentembed") await ctx.channel.send(embed=filesentembed) dev.log(s, f"send embed to dm\nend chatlog command execution")
async def on_command_error(self, ctx, error): #ctx is context of the command if isinstance( error, commands.MissingPermissions ): #If the command executor does not have permission to run the command missingpermissionembed = discord.Embed( #discord embed method title="Insufficient Permission!", description= "You do not have permission to execute this command.", color=discord.Color.red()) dev.log( s, f"user attempted to execute command but does not have permission" ) dev.log(s, f"{error}") await ctx.send(embed=missingpermissionembed) elif isinstance(error, commands.MissingRequiredArgument): missingargumentembed = discord.Embed( title="Invalid Arguments!", description= "One or more of the command arguments you provided were invalid. Did you type out the command correctly? Do ?help to see a list of commands.", color=discord.Color.red()) dev.log( s, f"user attempted to execute command but the arguments provided were not valid" ) await ctx.send(embed=missingargumentembed) elif isinstance(error, commands.CommandNotFound): invalidcommandembed = discord.Embed( title="Invalid Command!", description= "The command you entered is not a valid command. Run ?help to view a list of commands.", color=discord.Color.red()) dev.log( s, f"user attempted to execute command but it does not exist") dev.log(s, f"{error}") await ctx.send(embed=invalidcommandembed) elif isinstance(error, commands.NoPrivateMessage): return # botmissingpermsembed = discord.Embed( # title="I can't run this command!", # description="Due to the way you have configured my permissions, I am not allowed to run this command. Try dragging my role up in the role hierarchy!", # color = discord.Color.red() # ) # # await ctx.send(embed=botmissingpermsembed) else: unknownerrorembed = discord.Embed( title="Uh Oh! Something went wrong! :/", description= f"An unknown error has occured. If this keeps happening, contact an admin or developer and give them this info: `{error}`", color=discord.Color.red()) await ctx.send(embed=unknownerrorembed) dev.log(s, f"an unknown error occured") dev.log(s, f"{error}") errorlog = open("errors.log", "w+") errorlog.write(str(error)) print(str(error)) errorlog.close()
async def postfact(): dev.log(s, f"running postfact") factcount = open("factcount.txt", "r") dev.log(s, f"opened factcount.txt") currentfact = int(factcount.read()) dev.log(s, f"read {currentfact}") nextfact = currentfact + 1 dev.log(s, f"next fact will be {nextfact}") factcount.close() dev.log(s, f"closing factcount") factcount = open("factcount.txt", "w") dev.log(s, f"open factcount in write mode") factcount.write(str(nextfact)) dev.log(s, f"write {nextfact} to factcount.txt") factcount.close() fotd_channel = client.get_channel(703463111155253329) dev.log(s, f"got channel") raw_fact = urllib.request.urlopen( "https://uselessfacts.jsph.pl/random.json?language=en") dev.log(s, f"requested url") fact = json.loads(raw_fact.read()) dev.log(s, f"read {fact}") await fotd_channel.send(f'[**Fact #{str(currentfact)}**] {fact["text"]}') dev.log(s, f"send {fact['text']}")
fact = json.loads(raw_fact.read()) dev.log(s, f"read {fact}") await fotd_channel.send(f'[**Fact #{str(currentfact)}**] {fact["text"]}') dev.log(s, f"send {fact['text']}") @client.event #event decorator, makes event handling a lot easier async def on_ready(): await client.change_presence(activity=discord.Activity( type=discord.ActivityType.playing, name="Version c3.2.3")) #set the bot's playing status dev.log( s, f"logged in as {client.user.name}#{client.user.discriminator} with latency {round(client.latency*1000)}ms. THIS IS A CANARY BUILD!" ) dev.log( s, f"bot running in {__file__}") #get the directory the bot is running in postfact.start() for filename in os.listdir('./modules'): #Get all files in current directory dev.log(s, f"detected {filename}") if filename.endswith(".py"): #Check if python file client.load_extension( f"modules.{filename[:-3]}") #Load the module (/modules/ folder) dev.log(s, f"successfully loaded module {filename}") client.run("NzAzNDU4MzczMzAzMTQwNDAy.XqO44Q.uPPBr9JPUY0FcGTyaKnFQOV3CVQ" ) #Login to discord API