Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Gunale0926/KO-ON-Bot
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.0.0
Choose a base ref
...
head repository: Gunale0926/KO-ON-Bot
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 14 commits
  • 8 files changed
  • 3 contributors

Commits on Aug 31, 2022

  1. Update README.org

    Gunale0926 authored Aug 31, 2022
    Copy the full SHA
    5e26acf View commit details

Commits on Sep 1, 2022

  1. fix bug

    nick-haoran committed Sep 1, 2022
    1
    Copy the full SHA
    c7058e9 View commit details
  2. gitignore

    nick-haoran committed Sep 1, 2022
    Copy the full SHA
    b9fa608 View commit details
  3. Copy the full SHA
    aa2bbcf View commit details
  4. Update README.org

    Gunale0926 authored Sep 1, 2022
    Copy the full SHA
    21e6c55 View commit details

Commits on Sep 6, 2022

  1. v2.0.2

    nick-haoran committed Sep 6, 2022
    Copy the full SHA
    28445ec View commit details
  2. Copy the full SHA
    6b79557 View commit details
  3. voicepatch

    nick-haoran committed Sep 6, 2022
    Copy the full SHA
    6f2f03d View commit details

Commits on Sep 25, 2022

  1. v204

    nick-haoran committed Sep 25, 2022
    Copy the full SHA
    04f1d0c View commit details

Commits on Nov 6, 2022

  1. v205

    nick-haoran committed Nov 6, 2022
    Copy the full SHA
    44555cb View commit details

Commits on Jan 8, 2023

  1. Update README.org

    Gunale0926 authored Jan 8, 2023
    Copy the full SHA
    ad5f13d View commit details

Commits on Jan 10, 2023

  1. Bump certifi from 2022.5.18.1 to 2022.12.7

    Bumps [certifi](https://github.com/certifi/python-certifi) from 2022.5.18.1 to 2022.12.7.
    - [Release notes](https://github.com/certifi/python-certifi/releases)
    - [Commits](certifi/python-certifi@2022.05.18.1...2022.12.07)
    
    ---
    updated-dependencies:
    - dependency-name: certifi
      dependency-type: direct:production
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Jan 10, 2023
    Copy the full SHA
    fdf10f5 View commit details
  2. Merge pull request #22 from Gunale0926/dependabot/pip/certifi-2022.12.7

    Bump certifi from 2022.5.18.1 to 2022.12.7
    Gunale0926 authored Jan 10, 2023
    Copy the full SHA
    c2b2cdb View commit details
  3. Update README.org

    Gunale0926 authored Jan 10, 2023
    Copy the full SHA
    d2295d5 View commit details
Showing with 290 additions and 175 deletions.
  1. +3 −0 .gitignore
  2. +5 −3 README.org
  3. +120 −103 core.py
  4. +27 −33 music_manage.py
  5. +1 −1 requirements.txt
  6. +1 −1 scheduler.py
  7. +59 −13 status_manage.py
  8. +74 −21 voiceAPI.py
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -139,3 +139,6 @@ webtest.py
webtest1.py

.DS_Store
ws.py
.vscode/settings.json
test.py
8 changes: 5 additions & 3 deletions README.org
Original file line number Diff line number Diff line change
@@ -11,21 +11,23 @@ git clone git@github.com:Gunale0926/KO-ON-Bot.git --recursive
python3 -m pip install -r KO-ON-Bot/requirements.txt
#+END_SRC
*** Config
1. Rename ~config.tmp.json~ to config.json
1. Rename ~config.tmp.json~ to ~config.json~
2. Fill Keywords with value
- ~token*~: KOOK Bot token for bot *
- ~default_platform*~ (Optional): Default Platform for Bot *
- ~n_*~: configs for Netease Music
- ~b_*~: configs for Bilibili
- ~qq_enable~: "1" or "0"
- ~qq_enable~: ~1~ or ~0~
- ~q_*~: configs for QQ Music
- ~schedule_*~: configs for scheduler(refer to the template)
*** Run the BOT
**** Windows
#+BEGIN_SRC bash
start /main run.bat
#+END_SRC
**** Linux & macOS
**** macOS
#+BEGIN_SRC bash
sh run.sh
#+END_SRC
**** Linux
https://github.com/Gunale0926/KO-ON-Bot/issues/19#issue-1517067767
223 changes: 120 additions & 103 deletions core.py
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@
lyrics = {}
task_id = {}
add_LOCK = {}
deletelist = set()


def run():
@@ -62,6 +63,7 @@ def run():
rtcpport = botid + '234'

bot = Bot(token=config['token' + botid])
bot.client.ignore_self_msg=False
logger = getLogger('bot')
logger.setLevel(level=WARNING)

@@ -79,6 +81,8 @@ def run():

logger.addHandler(file_handler)
logger.addHandler(stream_handler)
join_command = status_manage.custom_joincommand(config, botid, logger)
timeout_threshold = status_manage.custom_timeout(config, logger)
default_platform = status_manage.custom_preferred_platform(
config, botid, logger)

@@ -507,7 +511,7 @@ def get_post_msg(msg: Message, voiceid: str):
"voicechannel": voiceid,
"botid": [botid],
"able": [len(voice) < 2],
"already_in": [msg.ctx.guild.id in list(playlist.keys())]
"already_in": [msg.ctx.guild.id in list(voice.keys())]
}

async def init_guild(msg: Message, voiceid: str):
@@ -526,66 +530,76 @@ async def init_guild(msg: Message, voiceid: str):
await msg.ctx.channel.send("已加入频道")
voice[msg.ctx.guild.id] = Voice(config['token' + botid])

# @bot.command(name=join_command)
# async def connect(msg: Message):
# global playlist
# global p
# global port
# global rtcpport
# global channel
# global voice
# global voicechannelid
# global msgid
# global JOINLOCK
# global pop_now
# while JOINLOCK:
# await sleep(0.1)
# JOINLOCK = True
# try:
# if len(voice) >= 2 and channel.get(msg.ctx.guild.id, -1) == -1:
# await msg.ctx.channel.send(
# "播放槽位已满,请加入交流服务器获取备用机 https://kook.top/vyAPVw")
# JOINLOCK = False
# return
# voiceid = await msg.ctx.guild.fetch_joined_channel(msg.author)
# try:
# voiceid = voiceid[0].id
# except:
# await msg.ctx.channel.send("请先进入一个语音频道或退出重进")
# JOINLOCK = False
# return
# logger.warning(voiceid)
# try:
# task_id[msg.ctx.guild.id] = -1
# singleloops[msg.ctx.guild.id] = 0
# timeout[msg.ctx.guild.id] = 0
# LOCK[msg.ctx.guild.id] = False
# pop_now[msg.ctx.guild.id] = False
# playlist[msg.ctx.guild.id] = []
# voicechannelid[msg.ctx.guild.id] = voiceid
# channel[msg.ctx.guild.id] = msg.ctx.channel
# playtime[msg.ctx.guild.id] = 0
# msgid[msg.ctx.guild.id] = "0"
# duration[msg.ctx.guild.id] = 0
# port[msg.ctx.guild.id] = rtcpport
# await msg.ctx.channel.send("已加入频道")
# voice[msg.ctx.guild.id] = Voice(config['token' + botid])
# event_loop = get_event_loop()

# ensure_future(status_manage.start(voice[msg.ctx.guild.id],
# voiceid, msg.ctx.guild.id,
# voiceffmpeg, port, logger),
# loop=event_loop)
# rtcpport = str(int(rtcpport) + 1)
# await bot.client.update_listening_music(
# f"已用槽位:{str(len(voice))}", "KO-ON",
# SoftwareTypes.CLOUD_MUSIC)
# JOINLOCK = False
# except:
# JOINLOCK = False
# except:
# JOINLOCK = False
# JOINLOCK = False
@bot.command(name='退出语音')
async def exit_voice(msg: Message):
voiceid = await msg.ctx.guild.fetch_joined_channel(msg.author)
try:
if msg.ctx.channel.id != channel[msg.ctx.guild.id].id:
return
except:
return
try:
voiceid = voiceid[0].id
if voiceid != voicechannelid[msg.ctx.guild.id]:
await msg.ctx.channel.send("请先进入听歌频道或退出重进")
return
except:
await msg.ctx.channel.send("请先进入听歌频道或退出重进")
return
try:
deletelist.add(msg.ctx.guild.id)
await msg.ctx.channel.send("退出成功")
except:
pass

@bot.command(name=join_command)
async def connect(msg: Message):
global playlist
global p
global port
global rtcpport
global channel
global voice
global voicechannelid
global msgid
global JOINLOCK
global pop_now
while JOINLOCK:
await sleep(0.1)
JOINLOCK = True
try:
if len(voice) >= 2 and channel.get(msg.ctx.guild.id, -1) == -1:
await msg.ctx.channel.send(
"播放槽位已满,请加入交流服务器获取备用机 https://kook.top/vyAPVw")
JOINLOCK = False
return
voiceid = await msg.ctx.guild.fetch_joined_channel(msg.author)
try:
voiceid = voiceid[0].id
except:
await msg.ctx.channel.send("请先进入一个语音频道或退出重进")
JOINLOCK = False
return
logger.warning(voiceid)
try:
add_LOCK[msg.ctx.guild.id] = False
await init_guild(msg, voiceid)
event_loop = get_event_loop()

ensure_future(status_manage.start(voice[msg.ctx.guild.id],
voiceid, msg.ctx.guild.id,
voiceffmpeg, port, logger),
loop=event_loop)
rtcpport = str(int(rtcpport) + 1)
await bot.client.update_listening_music(
f"已用槽位:{str(len(voice))}", "KO-ON",
SoftwareTypes.CLOUD_MUSIC)
JOINLOCK = False
except:
JOINLOCK = False
except:
JOINLOCK = False
JOINLOCK = False

@bot.command(name='绑定点歌频道')
async def bind_text_channel(msg: Message):
@@ -829,7 +843,7 @@ async def prtlist(msg: Message):
@bot.command(name="帮助")
async def help(msg: Message):
await msg.ctx.channel.send(
status_manage.get_helpcm(default_platform))
status_manage.get_helpcm(default_platform, join_command))

@bot.command(name="状态")
async def status(msg: Message):
@@ -902,9 +916,10 @@ async def reconnect(msg: Message):
@bot.command(name="搜索")
async def musicsearch(msg: Message, *args):
global netease_cookie
if botid != '1':
return
async with ClientSession(connector=TCPConnector(ssl=False)) as session:
if botid != "1":
return
await msg.ctx.channel.send("请稍等,正在处理中")
headers = status_manage.get_netease_headers(netease_cookie)
song_name = ''
for st in args:
@@ -1005,7 +1020,6 @@ async def load_cache(botid: str, singleloops: dict, timeout: dict,
loop=event_loop)
rtcpport = str(int(rtcpport) + 1)
await sleep(0.3)

except:
logger.warning('load cache fail')

@@ -1026,6 +1040,7 @@ async def update_played_time_and_change_music():
global singleloops
global load
global pop_now
global deletelist
if firstlogin:
firstlogin = False
await status_manage.login(bot, botid, qq_enable, netease_phone,
@@ -1037,7 +1052,6 @@ async def update_played_time_and_change_music():
await load_cache(botid, singleloops, timeout, LOCK, playtime,
duration, port, voice, config, voiceffmpeg,
event_loop)
deletelist = []
savetag = False
for guild, songlist in playlist.items():

@@ -1047,19 +1061,12 @@ async def update_played_time_and_change_music():
if len(playlist[guild]) == 0:
logger.warning("timeout +7")
timeout[guild] += deltatime
if timeout[guild] > 60:
logger.warning("timeout auto leave")
async with ClientSession(connector=TCPConnector(
ssl=False)) as session:
await status_manage.delmsg(msgid[guild], config, botid,
session, logger)
await status_manage.disconnect(bot, guild, voice, timeout,
voiceffmpeg, LOCK, msgid,
voicechannelid, channel,
singleloops, playtime,
duration, port, pop_now,
task_id, add_LOCK, logger)
deletelist.append(guild)
if timeout[guild] > timeout_threshold:
try:
deletelist.add(guild)
logger.warning("timeout auto leave")
except:
pass
continue
else:
timeout[guild] = 0
@@ -1182,13 +1189,37 @@ async def update_played_time_and_change_music():
playlist[guild].pop(0)
playtime[guild] = 0
duration[guild] = 0
LOCK[guild] = False
for guild in deletelist:
savetag = True
del playlist[guild]
try:
savetag = True
await leave_voice_channel(guild)
del playlist[guild]
except:
pass
deletelist.clear()
if savetag:
await status_manage.save_cache(botid, playlist, voicechannelid,
msgid, channel)

async def leave_voice_channel(guild: str):
status_manage.kill(guild, p, logger)
async with ClientSession(connector=TCPConnector(ssl=False)) as session:
await status_manage.delmsg(msgid[guild], config, botid, session,
logger)
try:
for task in all_tasks():
if id(task) == task_id[guild]:
logger.warning('cancelling the task {}: {}'.format(
id(task), task.cancel()))
task_id[guild] = -1
except:
pass
await status_manage.disconnect(bot, guild, voice, timeout, voiceffmpeg,
LOCK, msgid, voicechannelid, channel,
singleloops, playtime, duration, port,
pop_now, task_id, add_LOCK, logger)

@bot.task.add_interval(minutes=30)
async def keep_login():
async with ClientSession(connector=TCPConnector(ssl=False)) as session:
@@ -1289,28 +1320,14 @@ async def on_btn_clicked(_: Bot, e: Event):
except:
LOCK[guild] = False

@bot.on_event(EventTypes.EXITED_CHANNEL)
async def on_exit_voice(_: Bot, e: Event):
guild = e.target_id
try:
while LOCK[guild]:
await sleep(0.1)
LOCK[guild] = True
global playlist
logger.warning(e.body)
if playlist[guild][0]['userid'] == e.body['user_id']:
pop_now[guild] = True
now = playlist[guild][0]
plcp = playlist[guild]
plcp.pop(0)
tmp = [
song for song in plcp if song['userid'] != e.body['user_id']
]
tmp.insert(0, now)
playlist[guild] = tmp
LOCK[guild] = False
except:
LOCK[guild] = False
@bot.task.add_interval(minutes=1)
async def auto_check_exit_voice():
async with ClientSession(connector=TCPConnector(ssl=False)) as session:
for guild_id,voice_id in voicechannelid.items():
response=await status_manage.vcch_usrlist(voice_id,config,botid,session)
if len(response['data'])<=1:
logger.warning(f'{guild_id} empty leave')
deletelist.add(guild_id)

bot.command.update_prefixes("")

Loading