def __init__(self, bot): self.bot = bot # load yaml data = (yamlhook("database.yaml").load()) print("-----------------------") print("Check Database ...") if (data == None) or ('shrimp' not in list(data.keys())) or (type( data['shrimp']) is not dict): # initialize data print("Found new Database! initializing...") data['shrimp'] = {} self.data = data['shrimp'] self.dump_yaml = yamlhook("database.yaml").Operate # check if shrimp exists async def shrimp_check(): await self.bot.wait_until_ready() while not self.bot.is_closed(): # check shrimp is full or not if (self.data != None): for i in list(self.data.keys()): self.data[i]['shrimpcount'] = int( self.data[i]['counter'] * 0.8) + 40 self.data[i]['counter'] = 0 self.dump_yaml(setting=self.data, dictTopic='shrimp') await asyncio.sleep(7200) # 2h self._shrimp_check = self.bot.loop.create_task(shrimp_check())
def __init__(self, bot: commands.Bot): # initialize setting self.bot = bot ydata = yamlhook("database.yaml").load() if (ydata == None or type(ydata['blacklist']) is not list): ydata['blacklist'] = [] yamlhook("database.yaml").Operate('blacklist', ydata['blacklist'])
async def black_list(self, ctx: commands.Context): ydata = yamlhook("database.yaml").load() blacklist = [bm for bm in ydata['blacklist']] # embed embedout = Embed(color=self.bot.default_colour) embedout.add_field(name='目前黑名單內的ID', value=f"{blacklist}", inline=False) await ctx.send(embed=embedout)
async def black_remove(self, ctx: commands.Context, member: discord.User): ydata = yamlhook("database.yaml").load() # open blacklist try: ydata['blacklist'].remove(member.id) # blacklist remove content yamlhook("database.yaml").Operate('blacklist', ydata['blacklist']) # 輸出移除成功 embedout = Embed(colour=self.bot.default_colour) embedout.add_field(name='✅ 成功', value=f"`{member}`已從黑名單移除!", inline=False) await ctx.send(embed=embedout) except ValueError: # 找不到ID embedout = Embed(colour=self.bot.default_colour) embedout.add_field(name='❌ 失敗', value=f"找不到`{member}`的資料。", inline=False) await ctx.send(embed=embedout)
async def black_add(self, ctx: commands.Context, member: discord.User): ydata = yamlhook("database.yaml").load() # open blacklist if member.id not in ydata['blacklist']: # 檢查一次,防止有兩個同樣的ID存在 ydata['blacklist'].append(member.id) # blacklist add content yamlhook("database.yaml").Operate('blacklist', ydata['blacklist']) # 輸出增加成功 embedout = Embed(colour=self.bot.default_colour) embedout.add_field(name='✅ 成功', value=f"`{member}`已增加到黑名單!", inline=False) await ctx.send(embed=embedout) else: embedout = Embed(colour=self.bot.default_colour) embedout.add_field(name='❌ 失敗', value=f"`{member}`已在黑名單內!", inline=False) await ctx.send(embed=embedout)
import discord from discord.ext import commands from discord.utils import get import asyncio from datahook import yamlhook ydata = yamlhook("config.yaml") class commandrole(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot self.rolesdata = ydata.load() self.dataOperate = ydata.Operate if ("commandrole" not in dict(self.rolesdata).keys()): self.dataOperate(dictTopic="commandrole", setting={}) # upload rolesdata self.rolesdata = ydata.load() # reload data # ------------------------------------------------------------------------------------------ def dataReload(self): self.rolesdata = ydata.load() # embed message send # ------------------------------------------------------------------------------------------
async def on_member_join(self, member: discord.User): # 被動Ban掉黑名單內的指定用戶 ydata = yamlhook("database.yaml").load() if member.id in ydata['blacklist']: await member.kick(reason=None) self.bot.sm_print(2, f"Detected {member}! has been kicked!")
async def on_connect(self): self.sm_print(1, "Connecting...") async def on_ready(self): await self.change_presence(activity=Game(name=f"[{self.prefix} help] This is test machine...")) self.sm_print(1, f"{self.user.name} is on!") async def error_process(self, ctx:commands.Context, error:commands.CommandError): member = ctx.author.name # user discriminator discriminator = ctx.author.discriminator # print to console self.sm_print(3, f"[{member}#{discriminator}]: {error}") ydata = yamlhook("config.yaml").load() bot = Bot(prefix=ydata['bot']['prefix']) # Load Cog def loadCog(): system("cls") for filename in listdir('./cmds'): if (filename.endswith('.py')): bot.load_extension(f'cmds.{filename[:-3]}') bot.sm_print(1,f"load [{filename[:-3]}] complete.") if __name__ == "__main__": loadCog() bot.sm_print(1, f"Using Prefix: {bot.prefix}") # 抓取 bot token bot.run(ydata['bot']['token'])