from datetime import datetime, timezone, timedelta from khl import TextMsg, Bot, Cert # load config from config/botConfig.json, replace `path` points to your own config file # config template: `./config/botConfig.json.example` with open('./config/botConfig.json', 'r', encoding='utf-8') as f: config = json.load(f) with open('./jsonTemplate/menu.json', 'r', encoding='utf-8') as menu: menuJson = json.load(menu) # init Cert and Bot cert = Cert(client_id=config['client_id'], client_secret=config['client_secret'], token=config['token']) bot = Bot(cmd_prefix=['.', '。'], cert=cert) matchBoard = dict() utc = timezone.utc beijing = timezone(timedelta(hours=8)) @bot.command(name='菜单') async def roll(msg: TextMsg): await msg.ctx.send_card_temp(menuJson) @bot.command(name='组队') async def roll(msg: TextMsg, t_info: str, t_time: str = 300): int_time = 0 try: int_time = int(t_time)
import datetime from khl.hardcoded import API_URL from config import getBotConfig, getToken, setFunctionSwitch, getFunctionSwitch, setToken, checkAdmin, unsetToken, operationPermission, checkPermission, operationFilter, setTellraw, getTellraw, getChannel from khl import TextMsg, Bot, Cert from config import setChannel, getServerConfig from serverUtils import getAllStatus, runCommand, addSnType botConfig = getBotConfig() cert = Cert(type=botConfig['type'], client_id=botConfig['client_id'], client_secret=botConfig['client_secret'], token=botConfig['token'], verify_token=botConfig['verify_token'], encrypt_key=botConfig['encrypt_key']) bot = Bot(cmd_prefix=botConfig['cmd_prefix'], cert=cert) async def reply(msg, text): if msg.guild_id not in botConfig[ 'limitGuild'] or msg.channel_id in botConfig['limitGuild'][ msg.guild_id]: await msg.reply(text) @bot.command(name='hello') async def hello(msg: TextMsg, *args): await reply( msg, f'GuildId: {msg.guild_id}\nChannelId: {msg.channel_id}\nAuthorId: {msg.author_id}' )
import json import random from khl import Message, Bot # load config from config/config.json, replace `path` to your own config file # config template: `./config/config.json.example` with open('../config/config.json', 'r', encoding='utf-8') as f: config = json.load(f) # init Bot bot = Bot(token=config['token']) # register command # invoke this via saying `!roll 1 100` in channel # or `/roll 1 100 5` to dice 5 times once @bot.command() async def roll(msg: Message, t_min: int, t_max: int, n: int = 1): result = [random.randint(t_min, t_max) for i in range(n)] await msg.reply(f'you got: {result}') # everything done, go ahead now! bot.run()
from khl import Bot, Message # init Bot bot = Bot(token='xxxxxxxxxxxxxxxxxxxxxxxxx') # register command # invoke this via saying `/hello` in channel @bot.command(name='hello') async def world(msg: Message ): # when `name` is not set, the function name will be used await msg.reply('world!') # everything done, go ahead now! bot.run()