refactor: reviewed all message send to channel

This commit is contained in:
Terry Geng 2020-03-28 13:13:20 +08:00
parent e695c36e5f
commit 112564ba45
3 changed files with 52 additions and 33 deletions

View File

@ -89,6 +89,23 @@ def send_multi_lines(bot, lines, text, linebreak="<br />"):
bot.send_msg(msg, text)
def send_multi_lines_in_channel(bot, lines, linebreak="<br />"):
global log
msg = ""
br = ""
for newline in lines:
msg += br
br = linebreak
if bot.mumble.get_max_message_length() \
and (len(msg) + len(newline)) > (bot.mumble.get_max_message_length() - 4): # 4 == len("<br>")
bot.send_channel_msg(msg)
msg = ""
msg += newline
bot.send_channel_msg(msg)
# ---------------- Variables -----------------
ITEMS_PER_PAGE = 50
@ -146,7 +163,7 @@ def cmd_url_ban(bot, user, text, command, parameter):
var.cache.free_and_delete(item.id)
var.playlist.remove_by_id(item.id)
else:
bot.send_msg(constants.strings('bad_parameter', command=command))
bot.send_msg(constants.strings('bad_parameter', command=command), text)
else:
bot.mumble.users[text.actor].send_text_message(constants.strings('not_admin'))
return
@ -202,7 +219,7 @@ def cmd_pause(bot, user, text, command, parameter):
global log
bot.pause()
bot.send_msg(constants.strings('paused'))
bot.send_channel_msg(constants.strings('paused'))
def cmd_play_file(bot, user, text, command, parameter, do_not_refresh_cache=False):
@ -243,7 +260,7 @@ def cmd_play_file(bot, user, text, command, parameter, do_not_refresh_cache=Fals
var.playlist.extend(music_wrappers)
send_multi_lines(bot, msgs, None)
send_multi_lines_in_channel(bot, msgs)
return
# try to do a partial match
@ -301,7 +318,7 @@ def cmd_play_file_match(bot, user, text, command, parameter, do_not_refresh_cach
if count != 0:
msgs.append("</ul>")
var.playlist.extend(music_wrappers)
send_multi_lines(bot, msgs, None, "")
send_multi_lines_in_channel(bot, msgs, "")
else:
if do_not_refresh_cache:
bot.send_msg(constants.strings("no_file"), text)
@ -330,7 +347,7 @@ def cmd_play_url(bot, user, text, command, parameter):
# If I am the second item on the playlist. (I am the next one!)
bot.async_download_next()
else:
bot.send_msg(constants.strings('bad_parameter', command=command))
bot.send_msg(constants.strings('bad_parameter', command=command), text)
def cmd_play_playlist(bot, user, text, command, parameter):
@ -378,7 +395,7 @@ def cmd_play_radio(bot, user, text, command, parameter):
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()))
else:
bot.send_msg(constants.strings('bad_url'))
bot.send_msg(constants.strings('bad_url'), text)
def cmd_rb_query(bot, user, text, command, parameter):
@ -502,7 +519,7 @@ def cmd_yt_search(bot, user, text, command, parameter):
msg = _yt_format_result(yt_last_result, yt_last_page * item_per_page, item_per_page)
bot.send_msg(constants.strings('yt_result', result_table=msg), text)
else:
bot.send_msg(constants.strings('yt_no_more'))
bot.send_msg(constants.strings('yt_no_more'), text)
# if query
else:
@ -515,7 +532,7 @@ def cmd_yt_search(bot, user, text, command, parameter):
msg = _yt_format_result(results, 0, item_per_page)
bot.send_msg(constants.strings('yt_result', result_table=msg), text)
else:
bot.send_msg(constants.strings('yt_query_error'))
bot.send_msg(constants.strings('yt_query_error'), text)
else:
bot.send_msg(constants.strings('bad_parameter', command=command), text)
@ -541,7 +558,7 @@ def cmd_yt_play(bot, user, text, command, parameter):
url = "https://www.youtube.com/watch?v=" + yt_last_result[0][0]
cmd_play_url(bot, user, text, command, url)
else:
bot.send_msg(constants.strings('yt_query_error'))
bot.send_msg(constants.strings('yt_query_error'), text)
else:
bot.send_msg(constants.strings('bad_parameter', command=command), text)
@ -612,7 +629,7 @@ def cmd_volume(bot, user, text, command, parameter):
if parameter and parameter.isdigit() and 0 <= int(parameter) <= 100:
bot.volume_set = float(float(parameter) / 100)
bot.send_msg(constants.strings('change_volume',
volume=int(bot.volume_set * 100), user=bot.mumble.users[text.actor]['name']))
volume=int(bot.volume_set * 100), user=bot.mumble.users[text.actor]['name']), text)
var.db.set('bot', 'volume', str(bot.volume_set))
log.info('cmd: volume set to %d' % (bot.volume_set * 100))
else:
@ -818,7 +835,7 @@ def cmd_repeat(bot, user, text, command, parameter):
)
log.info("bot: add to playlist: " + music.format_debug_string())
bot.send_msg(constants.strings("repeat", song=music.format_song_string(), n=str(repeat)), text)
bot.send_channel_msg(constants.strings("repeat", song=music.format_song_string(), n=str(repeat)))
def cmd_mode(bot, user, text, command, parameter):
@ -859,7 +876,7 @@ def cmd_play_tags(bot, user, text, command, parameter):
if count != 0:
msgs.append("</ul>")
var.playlist.extend(music_wrappers)
send_multi_lines(bot, msgs, None, "")
send_multi_lines_in_channel(bot, msgs, "")
else:
bot.send_msg(constants.strings("no_file"), text)
@ -1011,7 +1028,7 @@ def cmd_search_library(bot, user, text, command, parameter):
music_wrapper = get_cached_wrapper(items[0], user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()))
bot.send_channel_msg(constants.strings('file_added', item=music_wrapper.format_song_string()))
else:
for item in items:
count += 1
@ -1049,7 +1066,7 @@ def cmd_shortlist(bot, user, text, command, parameter):
var.playlist.extend(music_wrappers)
msgs.append("</ul>")
send_multi_lines(bot, msgs, None, "")
send_multi_lines_in_channel(bot, msgs, "")
return
try:
@ -1077,7 +1094,7 @@ def cmd_shortlist(bot, user, text, command, parameter):
var.playlist.extend(music_wrappers)
msgs.append("</ul>")
send_multi_lines(bot, msgs, None, "")
send_multi_lines_in_channel(bot, msgs, "")
return
elif len(indexes) == 1:
index = indexes[0]
@ -1087,7 +1104,7 @@ def cmd_shortlist(bot, user, text, command, parameter):
music_wrapper = get_cached_wrapper_from_scrap(bot, **kwargs)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()))
bot.send_channel_msg(constants.strings('file_added', item=music_wrapper.format_song_string()))
return
bot.send_msg(constants.strings('bad_parameter', command=command), text)
@ -1123,7 +1140,7 @@ def cmd_delete_from_library(bot, user, text, command, parameter):
return
msgs.append("</ul>")
send_multi_lines(bot, msgs, None, "")
send_multi_lines_in_channel(bot, msgs, "")
return
elif len(indexes) == 1:
index = indexes[0]

View File

@ -183,7 +183,7 @@ class MumbleBot:
new_version = util.new_release_version()
if version.parse(new_version) > version.parse(self.version):
self.log.info("update: new version %s found, current installed version %s." % (new_version, self.version))
self.send_msg(constants.strings('new_version_found'))
self.send_channel_msg(constants.strings('new_version_found'))
else:
self.log.debug("update: no new version found.")
@ -305,14 +305,16 @@ class MumbleBot:
self.log.error("bot: command %s failed with error: %s\n" % (command_exc, error_traceback))
self.send_msg(constants.strings('error_executing_command', command=command_exc, error=error), text)
def send_msg(self, msg, text=None):
def send_msg(self, msg, text):
msg = msg.encode('utf-8', 'ignore').decode('utf-8')
# text if the object message, contain information if direct message or channel message
if not text:
own_channel = self.mumble.channels[self.mumble.users.myself['channel_id']]
own_channel.send_text_message(msg)
else:
self.mumble.users[text.actor].send_text_message(msg)
self.mumble.users[text.actor].send_text_message(msg)
def send_channel_msg(self, msg):
msg = msg.encode('utf-8', 'ignore').decode('utf-8')
own_channel = self.mumble.channels[self.mumble.users.myself['channel_id']]
own_channel.send_text_message(msg)
def is_admin(self, user):
list_admin = var.config.get('bot', 'admin').rstrip().split(';')
@ -333,7 +335,7 @@ class MumbleBot:
if var.config.get("bot", "when_nobody_in_channel") == "pause_resume":
self.resume()
elif var.config.get("bot", "when_nobody_in_channel") == "pause":
self.send_msg(constants.strings("auto_paused"))
self.send_channel_msg(constants.strings("auto_paused"))
elif len(own_channel.get_users()) == 1:
# if the bot is the only user left in the channel
@ -359,7 +361,7 @@ class MumbleBot:
self.log.info("bot: play music " + music_wrapper.format_debug_string())
if var.config.getboolean('bot', 'announce_current_music'):
self.send_msg(music_wrapper.format_current_playing())
self.send_channel_msg(music_wrapper.format_current_playing())
if var.config.getboolean('debug', 'ffmpeg'):
ffmpeg_debug = "debug"
@ -450,7 +452,7 @@ class MumbleBot:
self.log.error("bot: with ffmpeg error: %s", self.last_ffmpeg_err)
self.last_ffmpeg_err = ""
self.send_msg(constants.strings('unable_play', item=current.format_title()))
self.send_channel_msg(constants.strings('unable_play', item=current.format_title()))
var.playlist.remove_by_id(current.id)
var.cache.free_and_delete(current.id)
@ -462,7 +464,7 @@ class MumbleBot:
if not current.is_ready():
self.log.info("bot: current music isn't ready, start downloading.")
var.playlist.async_prepare(var.playlist.current_index)
self.send_msg(constants.strings('download_in_progress', item=current.format_title()))
self.send_channel_msg(constants.strings('download_in_progress', item=current.format_title()))
self.wait_for_ready = True
else:
var.playlist.remove_by_id(current.id)
@ -589,7 +591,7 @@ class MumbleBot:
uri, '-ac', '1', '-f', 's16le', '-ar', '48000', '-')
if var.config.getboolean('bot', 'announce_current_music'):
self.send_msg(var.playlist.current_item().format_current_playing())
self.send_channel_msg(var.playlist.current_item().format_current_playing())
self.log.info("bot: execute ffmpeg command: " + " ".join(command))
# The ffmpeg process is a thread

View File

@ -496,7 +496,7 @@
200 : function(data) {
if (data.ver !== playlist_ver) {
playlist_ver = data.ver;
updatePlaylist();
checkForPlaylistUpdate();
}
updateControls(data.empty, data.play, data.mode);
}
@ -601,9 +601,9 @@
expand_copy.addClass('playlist-item');
expand_copy.appendTo(playlist_table);
expand_copy.click(function(){
updatePlaylist();
playlist_range_from = real_from;
playlist_range_to = real_to;
checkForPlaylistUpdate();
});
expand_copy.show();
}
@ -983,7 +983,7 @@
data: data
});
updatePlaylist();
checkForPlaylistUpdate();
}
function deleteAllResults(){
@ -998,7 +998,7 @@
data: data
});
updatePlaylist();
checkForPlaylistUpdate();
updateResults();
}