1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-30 15:04:18 +00:00
royalnet/discordbot.py

1212 lines
56 KiB
Python
Raw Normal View History

2017-11-11 17:55:13 +00:00
import random
2018-02-26 15:38:47 +00:00
import re
2019-01-02 19:51:43 +00:00
# discord.py has a different name
# noinspection PyPackageRequirements
2017-10-27 11:38:32 +00:00
import discord
2019-01-02 19:51:43 +00:00
# noinspection PyPackageRequirements
2017-10-31 22:50:05 +00:00
import discord.opus
2019-01-02 19:51:43 +00:00
# noinspection PyPackageRequirements
2018-02-25 18:50:57 +00:00
import discord.voice_client
2017-11-01 18:23:11 +00:00
import functools
2017-11-09 19:34:18 +00:00
import sys
2017-10-27 11:38:32 +00:00
import db
2017-11-05 21:50:37 +00:00
import youtube_dl
2018-01-15 12:42:40 +00:00
import concurrent.futures
2018-02-25 18:50:57 +00:00
import typing
2018-02-25 19:47:12 +00:00
import os
2018-02-26 09:48:17 +00:00
import asyncio
import configparser
2018-04-09 20:55:48 +00:00
import async_timeout
import raven
2018-05-31 19:43:43 +00:00
import logging
2018-07-26 17:26:03 +00:00
import datetime
2018-08-08 15:48:57 +00:00
import sqlalchemy.exc
2018-09-17 22:26:01 +00:00
import coloredlogs
2018-11-26 14:46:47 +00:00
import errors
2018-11-28 14:47:08 +00:00
import math
2018-12-03 11:00:37 +00:00
import enum
2018-05-31 19:43:43 +00:00
2018-09-18 22:02:39 +00:00
logging.getLogger().disabled = True
2018-08-16 17:46:07 +00:00
logger = logging.getLogger(__name__)
2018-09-18 22:13:41 +00:00
os.environ["COLOREDLOGS_LOG_FORMAT"] = "%(asctime)s %(levelname)s %(name)s %(message)s"
2018-09-17 22:26:01 +00:00
coloredlogs.install(level="DEBUG", logger=logger)
2017-10-27 11:38:32 +00:00
2019-01-02 19:33:15 +00:00
# Number emojis from one to ten
number_emojis = [":one:",
":two:",
":three:",
":four:",
":five:",
":six:",
":seven:",
":eight:",
":nine:",
":keycap_ten:"]
2018-02-26 15:38:47 +00:00
2017-10-27 11:38:32 +00:00
# Init the event loop
loop = asyncio.get_event_loop()
2018-08-07 23:30:43 +00:00
# FFmpeg settings
ffmpeg_settings = {}
2018-08-08 12:09:57 +00:00
# Init the executor
executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)
class Succ:
"""All calls to this class return itself."""
def __bool__(self):
return False
2018-12-04 14:21:09 +00:00
2018-12-03 23:56:29 +00:00
def __getattr__(self, attr):
return Succ()
def __call__(self, *args, **kwargs):
return Succ()
def __str__(self):
return "succ"
def __repr__(self):
return "<Succ>"
2018-05-26 08:50:54 +00:00
class Video:
2018-12-04 14:21:09 +00:00
def __init__(self, enqueuer: typing.Optional[discord.Member] = None):
2018-12-03 11:00:37 +00:00
self.is_ready = False
self.name = None
2018-07-25 22:08:53 +00:00
self.enqueuer = enqueuer
2018-12-03 11:00:37 +00:00
self.audio_source = None
2018-05-26 08:50:54 +00:00
def __str__(self):
2018-12-03 15:53:03 +00:00
if self.name is None:
return "_Untitled_"
2018-12-03 11:00:37 +00:00
return self.name
def plain_text(self):
"""Title without formatting to be printed on terminals."""
2018-12-03 15:53:03 +00:00
if self.name is None:
return "Untitled"
2018-12-03 11:00:37 +00:00
return self.name
def database_text(self):
"""The text to be stored in the database for the stats. Usually the same as plain_text()."""
2018-12-03 15:53:03 +00:00
if self.name is None:
raise errors.VideoHasNoName()
2018-12-03 11:00:37 +00:00
return self.name
2018-09-17 22:19:35 +00:00
def __repr__(self):
2018-12-03 11:00:37 +00:00
return f"<Video {self.name} ({'' if self.is_ready else 'not '}ready) added by {self.enqueuer}>"
def ready_up(self):
"""Prepare the video for playback in some way. For example, download it."""
raise NotImplementedError()
def make_audio_source(self):
"""Create an AudioSource to be played through Discord, and store and return it."""
raise NotImplementedError()
2018-12-04 14:21:09 +00:00
2018-12-03 11:00:37 +00:00
def get_suggestion(self):
"""Get the next suggested video, to be used when the queue is in LoopMode.FOLLOW_SUGGESTION"""
raise NotImplementedError()
2019-01-02 19:51:43 +00:00
# this is not an abstract class pycharm wtf
# noinspection PyAbstractClass
2018-12-03 11:00:37 +00:00
class YoutubeDLVideo(Video):
"""A file sourcing from YoutubeDL."""
2018-12-04 14:21:09 +00:00
def __init__(self, url, enqueuer: typing.Optional[discord.Member] = None):
2018-12-03 11:00:37 +00:00
super().__init__(enqueuer)
self.url = url
self.info = None
def get_info(self):
"""Get info about the video."""
2018-12-03 15:53:03 +00:00
if self.info:
return
with youtube_dl.YoutubeDL({"quiet": True,
"ignoreerrors": True,
"simulate": True}) as ytdl:
data = ytdl.extract_info(url=self.url, download=False)
if data is None:
raise errors.VideoInfoExtractionFailed()
2018-12-03 23:56:29 +00:00
if "entries" in data:
2018-12-03 15:53:03 +00:00
raise errors.VideoIsPlaylist()
self.info = data
self.name = data.get("title")
2018-12-03 11:00:37 +00:00
2018-12-03 15:53:03 +00:00
def __str__(self):
if self.info is None:
return f"`{self.url}`"
return f"_{self.name}_"
2018-09-17 22:19:35 +00:00
2018-05-29 21:50:47 +00:00
def plain_text(self):
2018-12-03 15:53:03 +00:00
if self.info is None:
return self.url
if not self.name.isprintable():
return self.url
return self.name
2018-12-03 11:00:37 +00:00
def get_filename(self):
"""Generate the filename of the video."""
2018-12-03 23:56:29 +00:00
if self.info is None:
2018-12-03 15:53:03 +00:00
raise errors.VideoInfoUnknown()
2018-12-03 23:56:29 +00:00
return "./opusfiles/{}.opus".format(re.sub(r'[/\\?*"<>|!:]', "_", self.info.get("title", self.info["id"])))
2018-12-04 14:21:09 +00:00
2018-12-03 15:53:03 +00:00
def ready_up(self):
"""Download the video."""
# Skip download if it is already ready
if self.is_ready:
return
# Retrieve info about the video
self.get_info()
# Check if the file to download already exists
if os.path.exists(self.get_filename()):
self.is_ready = True
return
# Download the file
2018-12-03 15:53:03 +00:00
logger.info(f"Starting youtube_dl download of {repr(self)}")
with youtube_dl.YoutubeDL({"noplaylist": True,
"format": "best",
"postprocessors": [{
"key": 'FFmpegExtractAudio',
2018-08-18 15:36:44 +00:00
"preferredcodec": 'opus'
}],
2018-12-03 15:53:03 +00:00
"outtmpl": self.get_filename(),
"quiet": True}) as ytdl:
2018-12-17 00:51:32 +00:00
ytdl.download([self.url])
2018-12-03 15:53:03 +00:00
logger.info(f"Completed youtube_dl download of {repr(self)}")
self.is_ready = True
2018-12-03 15:53:03 +00:00
def make_audio_source(self):
if not self.is_ready:
raise errors.VideoIsNotReady()
self.audio_source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(self.get_filename(), **ffmpeg_settings))
return self.audio_source
2018-08-07 23:30:43 +00:00
2018-12-03 11:00:37 +00:00
class LoopMode(enum.Enum):
NORMAL = enum.auto()
LOOP_QUEUE = enum.auto()
LOOP_SINGLE = enum.auto()
FOLLOW_SUGGESTIONS = enum.auto()
2018-12-04 10:18:00 +00:00
AUTO_SHUFFLE = enum.auto()
LOOPING_SHUFFLE = enum.auto()
2018-10-10 17:27:24 +00:00
2018-12-04 16:55:43 +00:00
class VideoQueue:
2018-11-28 15:44:34 +00:00
"""The queue of videos to be played."""
def __init__(self):
self.list: typing.List[Video] = []
self.now_playing: typing.Optional[Video] = None
2018-12-03 11:00:37 +00:00
self.loop_mode = LoopMode.NORMAL
2018-10-10 17:27:24 +00:00
2018-11-28 15:44:34 +00:00
def __len__(self) -> int:
return len(self.list)
2018-12-03 11:00:37 +00:00
def __next__(self) -> Video:
video = self.next_video()
self.advance_queue()
return video
def __repr__(self) -> str:
return f"<VideoQueue of length {len(self)}>"
2018-12-04 14:21:09 +00:00
def add(self, video: Video, position: int = None) -> None:
2018-11-28 15:44:34 +00:00
if position is None:
self.list.append(video)
return
self.list.insert(position, video)
2018-12-04 14:21:09 +00:00
2018-12-03 11:00:37 +00:00
def advance_queue(self):
2018-12-04 10:18:00 +00:00
"""Advance the queue to the next video."""
2018-12-03 23:56:29 +00:00
if self.loop_mode == LoopMode.NORMAL:
2018-12-03 15:53:03 +00:00
try:
self.now_playing = self.list.pop(0)
except IndexError:
self.now_playing = None
2018-12-03 23:56:29 +00:00
elif self.loop_mode == LoopMode.LOOP_QUEUE:
2018-12-03 11:00:37 +00:00
self.add(self.list[0])
2018-12-03 15:53:03 +00:00
self.now_playing = self.list.pop(0)
2018-12-03 23:56:29 +00:00
elif self.loop_mode == LoopMode.LOOP_SINGLE:
2018-12-03 11:00:37 +00:00
pass
2018-12-03 23:56:29 +00:00
elif self.loop_mode == LoopMode.FOLLOW_SUGGESTIONS:
if self.now_playing is None:
2018-12-03 15:53:03 +00:00
self.now_playing = None
return
2018-12-04 16:55:43 +00:00
self.now_playing = self.now_playing.get_suggestion()
2018-12-04 10:18:00 +00:00
elif self.loop_mode == LoopMode.AUTO_SHUFFLE:
self.shuffle()
try:
self.now_playing = self.list.pop(0)
except IndexError:
self.now_playing = None
elif self.loop_mode == LoopMode.LOOPING_SHUFFLE:
self.shuffle()
self.add(self.list[0])
self.now_playing = self.list.pop(0)
2018-12-03 11:00:37 +00:00
2018-11-28 15:44:34 +00:00
def next_video(self) -> typing.Optional[Video]:
if len(self.list) == 0:
return None
return self.list[0]
def shuffle(self):
random.shuffle(self.list)
def clear(self):
2018-12-06 23:04:26 +00:00
self.list = []
2018-11-28 15:44:34 +00:00
2018-12-03 15:53:03 +00:00
def find_video(self, name: str) -> typing.Optional[Video]:
"""Returns the first video with a certain name."""
2018-11-28 15:44:34 +00:00
for video in self.list:
2018-12-03 15:53:03 +00:00
if name in video.name:
2018-11-28 15:44:34 +00:00
return video
return None
2018-12-03 11:00:37 +00:00
2018-12-04 14:21:09 +00:00
def not_ready_videos(self, limit: typing.Optional[int] = None):
2018-12-03 15:53:03 +00:00
"""Return the non-ready videos in the first limit positions of the queue."""
2018-12-04 14:21:09 +00:00
video_list = []
2018-12-27 22:43:07 +00:00
for video in (self.list[:limit] + ([self.now_playing] if self.now_playing else [])):
2018-12-03 15:53:03 +00:00
if not video.is_ready:
2018-12-04 14:21:09 +00:00
video_list.append(video)
return video_list
2018-11-28 15:44:34 +00:00
def __getitem__(self, index: int) -> Video:
"""Get an element from the list."""
return self.list[index]
2018-10-10 17:27:24 +00:00
2018-11-07 19:50:55 +00:00
def escape(message: str):
return message.replace("<", "&lt;").replace(">", "&gt;")
2018-08-08 12:09:57 +00:00
def command(func):
"""Decorator. Runs the function as a Discord command."""
async def new_func(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str], *args,
**kwargs):
if author is not None:
2019-01-02 19:21:06 +00:00
self.sentry.user_context({
2018-08-08 12:09:57 +00:00
"discord_id": author.id,
"username": f"{author.name}#{author.discriminator}"
})
else:
2019-01-02 19:21:06 +00:00
self.sentry.user_context({
2018-08-08 12:09:57 +00:00
"source": "Telegram"
})
try:
result = await func(self, channel=channel, author=author, params=params, *args, **kwargs)
except Exception:
ei = sys.exc_info()
2018-09-17 22:07:00 +00:00
# noinspection PyBroadException
2018-08-08 12:09:57 +00:00
try:
await channel.send(f"☢ **ERRORE DURANTE L'ESECUZIONE DEL COMANDO {params[0]}**\n"
f"Il comando è stato ignorato.\n"
f"Una segnalazione di errore è stata automaticamente mandata a Steffo.\n\n"
f"Dettagli dell'errore:\n"
f"```python\n"
f"{repr(ei[1])}\n"
f"```")
except Exception:
pass
2019-01-02 19:21:06 +00:00
self.sentry.captureException(exc_info=ei)
2018-08-08 12:09:57 +00:00
else:
return result
return new_func
def requires_connected_voice_client(func):
"""Decorator. Ensures the voice client is connected before running the command."""
async def new_func(self: "RoyalDiscordBot", channel: discord.TextChannel, author: discord.Member,
params: typing.List[str], *args, **kwargs):
for voice_client in self.voice_clients:
if voice_client.channel in self.main_guild.channels and voice_client.is_connected():
break
else:
await channel.send("⚠️ Non sono connesso alla cv!\n"
"Fammi entrare scrivendo `!cv` mentre sei in chat vocale.")
return
return await func(self, channel=channel, author=author, params=params, *args, **kwargs)
return new_func
def requires_rygdb(func, optional=False):
async def new_func(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str], *args,
**kwargs):
session = db.Session()
dbuser = await loop.run_in_executor(executor,
session.query(db.Discord)
.filter_by(discord_id=author.id)
.join(db.Royal)
.first)
await loop.run_in_executor(executor, session.close)
if not optional and dbuser is None:
await channel.send("⚠️ Devi essere registrato su Royalnet per poter utilizzare questo comando.")
return
return await func(self, channel=channel, author=author, params=params, dbuser=dbuser, *args, **kwargs)
return new_func
2018-08-07 23:30:43 +00:00
class RoyalDiscordBot(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.main_channel: typing.Optional[discord.TextChannel] = None
self.main_guild: typing.Optional[discord.Guild] = None
2018-08-08 12:09:57 +00:00
self.commands = {
"!ping": self.cmd_ping,
"!cv": self.cmd_cv,
"!summon": self.cmd_cv,
"!play": self.cmd_play,
2018-12-29 00:26:30 +00:00
"!alexaplay": self.cmd_play,
"!okgoogleplay": self.cmd_play,
"!heysiriplay": self.cmd_play,
2018-08-08 12:09:57 +00:00
"!p": self.cmd_play,
"!search": self.cmd_play,
"!file": self.cmd_play,
"!skip": self.cmd_skip,
"!s": self.cmd_skip,
2018-11-06 22:11:35 +00:00
"!next": self.cmd_skip,
2018-08-08 12:09:57 +00:00
"!remove": self.cmd_remove,
2018-10-02 22:18:41 +00:00
"!r": self.cmd_remove,
2018-08-08 12:09:57 +00:00
"!cancel": self.cmd_remove,
"!queue": self.cmd_queue,
"!q": self.cmd_queue,
"!shuffle": self.cmd_shuffle,
"!clear": self.cmd_clear,
"!register": self.cmd_register,
"!radiomessages": self.cmd_radiomessages,
"!yes": self.null,
2018-08-17 13:09:03 +00:00
"!no": self.null,
"!pause": self.cmd_pause,
2018-12-04 10:18:00 +00:00
"!resume": self.cmd_resume,
2018-12-06 23:31:12 +00:00
"!loop": self.cmd_mode,
"!l": self.cmd_mode,
"!mode": self.cmd_mode,
"!m": self.cmd_mode
2018-08-08 12:09:57 +00:00
}
2018-11-28 15:44:34 +00:00
self.video_queue: VideoQueue = VideoQueue()
2018-11-28 14:47:08 +00:00
self.load_config("config.ini")
2019-01-02 19:21:06 +00:00
if self.sentry_token:
self.sentry = raven.Client(self.sentry_token,
release=raven.fetch_git_sha(os.path.dirname(__file__)),
install_logging_hook=False,
hook_libraries=[])
else:
logger.warning("Sentry not set, ignoring all calls to it.")
self.sentry = Succ()
2018-09-14 23:05:19 +00:00
self.inactivity_timer = 0
2018-08-07 23:30:43 +00:00
2018-12-03 23:56:29 +00:00
# noinspection PyAttributeOutsideInit
2018-11-28 14:47:08 +00:00
def load_config(self, filename):
2018-12-03 23:56:29 +00:00
# Init the config reader
config = configparser.ConfigParser()
2019-01-02 19:33:35 +00:00
config.read(filename)
2018-12-03 23:56:29 +00:00
# Token
try:
self.token = config["Discord"]["bot_token"]
except (KeyError, ValueError):
raise errors.InvalidConfigError("Missing Discord bot token.")
# Main channels, will be fully loaded when ready
try:
self.main_guild_id = int(config["Discord"]["server_id"])
self.main_channel_id = int(config["Discord"]["main_channel"])
except (KeyError, ValueError):
raise errors.InvalidConfigError("Missing main guild and channel ids.")
# Max enqueable video duration
# Defined in the YoutubeDLVideo class
# Max videos to predownload
try:
self.max_videos_to_predownload = int(config["Video"]["cache_size"])
except (KeyError, ValueError):
logger.warning("Max videos to predownload is not set, setting it to infinity.")
self.max_videos_to_predownload = None
# Max time to ready a video
try:
self.max_video_ready_time = int(config["Video"]["max_ready_time"])
except (KeyError, ValueError):
logger.warning("Max time to ready a video is not set, setting it to infinity. ")
self.max_video_ready_time = math.inf
# Radio messages
try:
2019-01-02 19:33:15 +00:00
if config["Discord"]["radio_messages_enabled"] == "True":
self.radio_messages = ["https://www.youtube.com/watch?v=3-yeK1Ck4yk",
"https://youtu.be/YcR7du_A1Vc",
"https://clyp.it/byg3i52l"]
try:
self.radio_messages_every = int(config["Discord"]["radio_messages_every"])
self.radio_messages_next_in = self.radio_messages_every
except (KeyError, ValueError):
logger.warning("Radio messages config error, disabling them.")
self.radio_messages = []
self.radio_messages_every = math.inf
self.radio_messages_next_in = math.inf
else:
logger.info("Radio messages are force-disabled.")
self.radio_messages = []
self.radio_messages_every = math.inf
self.radio_messages_next_in = math.inf
2018-12-03 23:56:29 +00:00
except (KeyError, ValueError):
logger.warning("Radio messages config error, disabling them.")
2019-01-02 19:33:15 +00:00
self.radio_messages = []
2018-12-03 23:56:29 +00:00
self.radio_messages_every = math.inf
self.radio_messages_next_in = math.inf
# Activity reporting
try:
self.activity_report_sample_time = int(config["Discord"]["activityreport_sample_time"])
except (KeyError, ValueError):
logger.warning("Activity reporting config error, disabling it.")
self.activity_report_sample_time = math.inf
2019-01-02 19:21:06 +00:00
# Sentry error reporting
try:
self.sentry_token = config["Sentry"]["token"]
except (KeyError, ValueError):
logger.warning("Sentry client config error, disabling it.")
self.sentry_token = None
2019-01-02 19:44:10 +00:00
# Easter eggs
try:
if config["Discord"]["song_text_easter_eggs_enabled"] == "True":
self.song_text_easter_eggs = {
"despacito": ":arrow_forward: this is so sad. alexa play {song}",
"faded": ":arrow_forward: Basta Garf, lasciami ascoltare {song}",
"ligma": ":arrow_forward: What is ligma? {song}!",
"sugma": ":arrow_forward: What is sugma? {song}!",
"sugondese": ":arrow_forward: What is sugondese? {song}!",
"bofa": ":arrow_forward: What is bofa? {song}!",
"updog": ":arrow_forward: What's up, dog? {song}!",
"sayo-nara": ":arrow_forward: I gently open the door. {song} awaits me inside.",
"monika": ":arrow_forward: Just Monika. Just Monika. Just {song}.",
"country road": ":arrow_forward: Take me home, to {song}, the place I belong!",
"never gonna give you up": ":arrow_forward: Rickrolling in 2018. Enjoy {song}!",
"september": ":arrow_forward: Do you remember? {song}.",
"homestuck": ":arrow_forward: > Enter song name. {song}",
"undertale": ":arrow_forward: Howdy! I'm Flowey! Listen to this friendly song: {song}",
"pumped up kicks": ":arrow_forward: Non metterti mica in testa strane idee ascoltando {song}...",
"jesus": ":arrow_forward: Respawn in 3 giorni. Intanto, ascolta {song}.",
"through The fire And flames": ":arrow_forward: Fai {song} su osu!, se ne sei capace!",
"slow clap": ":arrow_forward: :clap: :clap: :clap: {song} :clap: :clap: :clap:",
"pub scrubs": ":arrow_forward: MAV COME BACK WE MISS {song}!",
"alleluia": ":arrow_forward: Wah. Waaaah. Waluigi tiime: {song}",
"wah": ":arrow_forward: Wah. Waaaah. Waluigi tiime: {song}",
"waluigi": ":arrow_forward: Wah. Waaaah. Waluigi tiime: {song}",
"nyan cat": ":arrow_forward: Meow! :3 {song}",
"dragonborn": ":arrow_forward: FUS RO {song}!",
"dovahkiin": ":arrow_forward: FUS RO {song}!",
"initial d": ":arrow_forward: Guarda mamma sto driftando sul balcone di Balu grazie a {song}!",
"persona": ":arrow_forward: You'll never see {song} comiiiiing!",
"flamingo": ":arrow_forward: How many {song} do you have to eat?",
"linkin park": ":arrow_forward: Crawling in my {song}!",
"magicite": "⚠️ Warning: {song} contiene numerosi bug. E' ora in riproduzione.",
"papers please": ":arrow_forward: Glory to Arstotzka! {song}!",
"we are number one": ":arrow_forward: Now paying respect to Robbie Rotten: {song}",
"jump up superstar": ":arrow_forward: Is {song} the Tengen Toppa Guren Lagann opening?",
"the world revolving": ":arrow_forward: CHAOS! CHAOS! I CAN DO {song}!",
"deltarune": ":arrow_forward: You hug Ralsei. A strange music starts playing: {song}",
"song of unhealing": ":arrow_forward: BEN {song}",
"police academy": ":arrow_forward: {song} - freedom.png",
"super smash bros. ultimate": ":arrow_forward: Re-awaken the undying light with {song}!",
"powerwolf": ":arrow_forward: Spaggia, ma non ti sei un po' stancato di {song}?",
"eurobeat": ":arrow_forward: Nemesis approva la scelta di {song}. Ben fatto, amico.",
"k/da": ":arrow_forward: Che noia...\n"
"Non ci si può nemmeno divertire con {song} che c'è qualcuno che se ne lamenta.\n"
"La prossima volta, metti qualcosa di diverso, per piacere.",
2019-01-02 19:51:43 +00:00
"youtube rewind": ":arrow_forward: Perchè ti vuoi così male?"
" Sigh, ascolta, discutere con te è inutile."
2019-01-02 19:44:10 +00:00
" Ti lascio qui {song}. Richiamami quando sarà tutto finito."
}
else:
self.song_text_easter_eggs = {}
except (KeyError, ValueError):
logger.warning("Song text easter egg information not found, defaulting to disabled.")
self.song_text_easter_eggs = {}
2018-11-28 14:47:08 +00:00
2018-12-03 23:56:29 +00:00
# noinspection PyAsyncCall
2018-08-07 23:30:43 +00:00
async def on_ready(self):
# Get the main guild
2018-11-28 14:47:08 +00:00
self.main_guild = self.get_guild(self.main_guild_id)
2018-08-07 23:30:43 +00:00
if not isinstance(self.main_guild, discord.Guild):
2018-11-26 14:46:47 +00:00
raise errors.InvalidConfigError("The main guild does not exist!")
2018-11-28 14:47:08 +00:00
# Get the main channel
self.main_channel = self.get_channel(self.main_channel_id)
if not isinstance(self.main_channel, discord.TextChannel):
raise errors.InvalidConfigError("The main channel is not a TextChannel!")
# Show yourself!
2018-08-07 23:30:43 +00:00
await self.change_presence(status=discord.Status.online, activity=None)
2018-09-18 23:04:48 +00:00
logger.info("Bot is ready!")
2018-11-28 14:47:08 +00:00
# Start the bot tasks
2018-11-18 16:45:14 +00:00
asyncio.ensure_future(self.queue_predownload_videos())
asyncio.ensure_future(self.queue_play_next_video())
asyncio.ensure_future(self.inactivity_countdown())
asyncio.ensure_future(self.activity_task())
2018-08-07 23:30:43 +00:00
async def on_message(self, message: discord.Message):
if message.channel != self.main_channel or message.author.bot:
return
2019-01-02 19:21:06 +00:00
self.sentry.user_context({
2018-08-07 23:30:43 +00:00
"discord": {
"discord_id": message.author.id,
"name": message.author.name,
"discriminator": message.author.discriminator
}
})
if not message.content.startswith("!"):
2018-12-04 10:18:00 +00:00
await message.channel.send(f"⚠️ In questa chat sono consentiti solo comandi per il bot.\n"
2018-08-07 23:30:43 +00:00
f"Riinvia il tuo messaggio in un altro canale!")
await message.delete()
return
data = message.content.split(" ")
2018-08-08 12:09:57 +00:00
if data[0] not in self.commands:
2018-12-04 10:18:00 +00:00
await message.channel.send("⚠️ Comando non riconosciuto.")
2018-08-07 23:30:43 +00:00
return
2018-09-18 23:04:48 +00:00
logger.debug(f"Received command: {message.content}")
2019-01-02 19:21:06 +00:00
self.sentry.extra_context({
2018-09-05 17:48:34 +00:00
"command": data[0],
"message": message
})
2018-09-14 23:05:19 +00:00
self.inactivity_timer = 3600
2018-08-08 12:09:57 +00:00
await self.commands[data[0]](channel=message.channel,
author=message.author,
params=data)
2017-11-06 15:54:36 +00:00
2018-08-07 23:30:43 +00:00
async def on_error(self, event_method, *args, **kwargs):
ei = sys.exc_info()
2018-12-03 23:56:29 +00:00
logger.critical(f"Critical error: {repr(ei)}")
2018-09-13 16:38:21 +00:00
# noinspection PyBroadException
2018-08-07 23:30:43 +00:00
try:
2018-09-14 23:05:19 +00:00
await self.main_channel.send(f"☢️ **ERRORE CRITICO NELL'EVENTO** `{event_method}`!\n"
2018-08-07 23:30:43 +00:00
f"Il bot si è chiuso e si dovrebbe riavviare entro qualche minuto.\n"
f"Una segnalazione di errore è stata automaticamente mandata a Steffo.\n\n"
f"Dettagli dell'errore:\n"
f"```python\n"
2018-12-03 23:56:29 +00:00
f"{repr(ei)}\n"
2018-08-07 23:30:43 +00:00
f"```")
await self.change_presence(status=discord.Status.invisible)
await self.close()
2018-09-13 16:38:21 +00:00
except Exception:
logger.error(f"Double critical error: {sys.exc_info()}")
2018-08-07 23:30:43 +00:00
loop.stop()
2019-01-02 19:21:06 +00:00
self.sentry.captureException(exc_info=ei)
2018-08-07 23:30:43 +00:00
exit(1)
async def feed_pipe(self, connection):
await self.wait_until_ready()
while True:
msg = await loop.run_in_executor(executor, connection.recv)
2018-09-18 23:04:48 +00:00
logger.debug(f"Received from the Telegram-Discord pipe: {msg}")
2018-12-29 00:26:30 +00:00
full_cv = (msg == "get cv full")
if msg.startswith("get cv"):
2018-08-07 23:30:43 +00:00
discord_members = list(self.main_guild.members)
2018-08-28 13:48:12 +00:00
channels = {0: None}
members_in_channels = {0: []}
message = ""
# Find all the channels
for member in discord_members:
2018-08-28 13:49:29 +00:00
if member.voice is not None:
2018-08-28 13:48:12 +00:00
channel = members_in_channels.get(member.voice.channel.id)
if channel is None:
members_in_channels[member.voice.channel.id] = list()
channel = members_in_channels[member.voice.channel.id]
channels[member.voice.channel.id] = member.voice.channel
channel.append(member)
else:
members_in_channels[0].append(member)
# Edit the message, sorted by channel
2018-09-18 23:04:48 +00:00
for channel in sorted(channels, key=lambda c: -c):
2018-08-28 13:48:12 +00:00
members_in_channels[channel].sort(key=lambda x: x.nick if x.nick is not None else x.name)
if channel == 0:
2018-09-18 23:04:48 +00:00
message += "<b>Non in chat vocale:</b>\n"
2018-08-28 13:48:12 +00:00
else:
2018-11-07 19:50:55 +00:00
message += f"<b>In #{escape(channels[channel].name)}:</b>\n"
2018-08-28 13:48:12 +00:00
for member in members_in_channels[channel]:
2018-09-18 23:04:48 +00:00
# Ignore not-connected non-notable members
2018-12-29 00:26:30 +00:00
if not full_cv and channel == 0 and len(member.roles) < 2:
2018-09-18 23:04:48 +00:00
continue
# Ignore offline members
2018-08-28 13:51:06 +00:00
if member.status == discord.Status.offline and member.voice is None:
2018-08-28 13:48:12 +00:00
continue
# Online status emoji
if member.bot:
message += "🤖 "
elif member.status == discord.Status.online:
message += "🔵 "
elif member.status == discord.Status.idle:
message += "⚫️ "
elif member.status == discord.Status.dnd:
message += "🔴 "
elif member.status == discord.Status.offline:
message += "⚪️ "
# Voice
if channel != 0:
# Voice status
if member.voice.self_deaf:
message += f"🔇 "
elif member.voice.self_mute:
message += f"🔈 "
else:
message += f"🔊 "
# Nickname
if member.nick is not None:
2018-11-07 19:50:55 +00:00
message += escape(member.nick)
2018-08-28 13:48:12 +00:00
else:
2018-11-07 19:50:55 +00:00
message += escape(member.name)
2018-08-28 13:48:12 +00:00
# Game or stream
if member.activity is not None:
2018-09-18 23:04:48 +00:00
if member.activity.type == discord.ActivityType.playing:
2018-11-07 19:50:55 +00:00
message += f" | 🎮 {escape(member.activity.name)}"
2018-09-18 23:04:48 +00:00
# Rich presence
2018-09-19 16:20:56 +00:00
try:
2018-11-06 22:11:35 +00:00
if member.activity.state is not None:
2019-01-02 19:51:43 +00:00
message += f" ({escape(member.activity.state)}" \
f" | {escape(member.activity.details)})"
2018-09-19 16:20:56 +00:00
except AttributeError:
2018-12-29 00:50:37 +00:00
pass
2018-09-18 23:04:48 +00:00
elif member.activity.type == discord.ActivityType.streaming:
2018-11-07 19:50:55 +00:00
message += f" | 📡 [{escape(member.activity.name)}]({escape(member.activity.url)})"
2018-09-18 23:04:48 +00:00
elif member.activity.type == discord.ActivityType.listening:
2018-11-07 19:50:55 +00:00
message += f" | 🎧 {escape(member.activity.name)}"
2018-08-28 13:48:12 +00:00
elif member.activity.type == discord.ActivityType.watching:
2018-11-07 19:50:55 +00:00
message += f" | 📺 {escape(member.activity.name)}"
2018-08-28 13:48:12 +00:00
message += "\n"
message += "\n"
connection.send(message)
2018-09-18 23:04:48 +00:00
logger.debug(f"Answered successfully cvlist request.")
2018-08-07 23:30:43 +00:00
elif msg.startswith("!"):
data = msg.split(" ")
2018-08-08 12:09:57 +00:00
if data[0] not in self.commands:
2018-08-07 23:30:43 +00:00
connection.send("error")
continue
2018-09-18 23:04:48 +00:00
logger.debug(f"Received command: {msg}")
2018-08-08 12:09:57 +00:00
await self.main_channel.send(f"{msg}\n"
f"_(da Telegram)_")
2018-09-05 17:48:34 +00:00
await self.commands[data[0]](channel=self.main_channel,
2018-08-08 12:09:57 +00:00
author=None,
params=data)
2018-08-07 23:30:43 +00:00
connection.send("success")
2018-02-25 18:50:57 +00:00
2018-08-08 12:09:57 +00:00
async def queue_predownload_videos(self):
while True:
2018-12-03 15:53:03 +00:00
await asyncio.sleep(1)
# Might have some problems with del
2018-12-24 20:35:03 +00:00
for index, video in enumerate(self.video_queue.not_ready_videos(self.max_videos_to_predownload)):
2018-08-08 12:09:57 +00:00
try:
2018-12-03 15:53:03 +00:00
with async_timeout.timeout(self.max_video_ready_time):
2018-12-03 23:56:29 +00:00
await loop.run_in_executor(executor, video.ready_up)
2018-08-08 12:09:57 +00:00
except asyncio.TimeoutError:
2018-12-04 14:21:09 +00:00
logger.warning(
f"Video {repr(video)} took more than {self.max_video_ready_time} to download, skipping...")
await self.main_channel.send(
2019-01-02 19:51:43 +00:00
f"⚠️ La preparazione di {video} ha richiesto più di {self.max_video_ready_time} secondi,"
f" pertanto è stato rimosso dalla coda.")
2018-11-28 15:44:34 +00:00
del self.video_queue.list[index]
2018-08-08 12:09:57 +00:00
continue
except Exception as e:
2019-01-02 19:21:06 +00:00
self.sentry.user_context({
2018-09-18 23:04:48 +00:00
"discord": {
"discord_id": video.enqueuer.id,
"name": video.enqueuer.name,
"discriminator": video.enqueuer.discriminator
}
})
2019-01-02 19:21:06 +00:00
self.sentry.extra_context({
2018-09-18 23:04:48 +00:00
"video": video.plain_text()
})
2019-01-02 19:21:06 +00:00
self.sentry.captureException()
2018-12-03 15:53:03 +00:00
logger.error(f"Uncaught video download error: {e}")
2018-08-08 12:09:57 +00:00
await self.main_channel.send(f"⚠️ E' stato incontrato un errore durante il download di "
f"{str(video)}, quindi è stato rimosso dalla coda.\n\n"
f"```python\n"
2018-12-03 23:56:29 +00:00
f"{str(e.args)}"
2018-08-08 12:09:57 +00:00
f"```")
2018-11-28 15:44:34 +00:00
del self.video_queue.list[index]
2018-08-08 12:09:57 +00:00
continue
2018-02-25 18:50:57 +00:00
2018-08-08 12:09:57 +00:00
async def queue_play_next_video(self):
await self.wait_until_ready()
while True:
2018-12-03 15:53:03 +00:00
await asyncio.sleep(1)
2018-08-08 12:09:57 +00:00
for voice_client in self.voice_clients:
2018-12-03 15:53:03 +00:00
# Do not add play videos if something else is playing!
if not voice_client.is_connected():
2018-08-08 12:09:57 +00:00
continue
2018-12-03 15:53:03 +00:00
if voice_client.is_playing():
2018-08-08 12:09:57 +00:00
continue
2018-12-03 15:53:03 +00:00
if voice_client.is_paused():
continue
2018-12-06 23:04:26 +00:00
# Ensure the next video is ready
2018-12-06 23:15:26 +00:00
next_video = self.video_queue.next_video()
2018-12-06 23:20:25 +00:00
if next_video is None or not next_video.is_ready:
2018-12-06 23:04:26 +00:00
continue
2018-12-03 15:53:03 +00:00
# Advance the queue
2018-12-03 23:56:29 +00:00
self.video_queue.advance_queue()
2018-12-03 15:53:03 +00:00
# Try to generate an AudioSource
2018-12-03 23:56:29 +00:00
if self.video_queue.now_playing is None:
2018-08-08 12:09:57 +00:00
continue
2018-12-06 23:04:26 +00:00
audio_source = self.video_queue.now_playing.make_audio_source()
2018-12-03 15:53:03 +00:00
# Start playing the AudioSource
2018-12-03 23:56:29 +00:00
logger.info(f"Started playing {self.video_queue.now_playing.plain_text()}.")
2018-08-08 12:09:57 +00:00
voice_client.play(audio_source)
2018-12-03 23:56:29 +00:00
# Update the voice_client activity
activity = discord.Activity(name=self.video_queue.now_playing.plain_text(),
2018-11-06 22:11:35 +00:00
type=discord.ActivityType.listening)
2018-12-03 23:56:29 +00:00
logger.debug("Updating bot presence...")
2018-08-08 12:09:57 +00:00
await self.change_presence(status=discord.Status.online, activity=activity)
2018-12-03 23:56:29 +00:00
# Record the played song in the database
if self.video_queue.now_playing.enqueuer is not None:
logger.debug(f"Adding {self.video_queue.now_playing.plain_text()} to db.PlayedMusic...")
2018-08-08 15:48:57 +00:00
try:
session = db.Session()
enqueuer = await loop.run_in_executor(executor, session.query(db.Discord)
2018-12-04 14:21:09 +00:00
.filter_by(
discord_id=self.video_queue.now_playing.enqueuer.id)
2018-08-08 15:48:57 +00:00
.one_or_none)
played_music = db.PlayedMusic(enqueuer=enqueuer,
2018-12-03 23:56:29 +00:00
filename=self.video_queue.now_playing.database_text(),
2018-08-08 15:48:57 +00:00
timestamp=datetime.datetime.now())
session.add(played_music)
await loop.run_in_executor(executor, session.commit)
await loop.run_in_executor(executor, session.close)
except sqlalchemy.exc.OperationalError:
pass
2018-12-03 23:56:29 +00:00
# Send a message in chat
2019-01-02 19:44:10 +00:00
for key in self.song_text_easter_eggs:
2018-12-03 23:56:29 +00:00
if key in self.video_queue.now_playing.name.lower():
2018-12-04 14:21:09 +00:00
await self.main_channel.send(
2019-01-02 19:44:10 +00:00
self.song_text_easter_eggs[key].format(song=str(self.video_queue.now_playing)))
2018-08-08 12:09:57 +00:00
break
else:
2018-12-04 14:21:09 +00:00
await self.main_channel.send(
f":arrow_forward: Ora in riproduzione: {str(self.video_queue.now_playing)}")
2018-08-08 12:09:57 +00:00
2018-09-14 23:05:19 +00:00
async def inactivity_countdown(self):
while True:
await asyncio.sleep(1)
if self.inactivity_timer > 0:
self.inactivity_timer -= 1
continue
for voice_client in self.voice_clients:
if voice_client.is_connected():
2018-09-18 23:04:48 +00:00
logger.info("Disconnecting due to inactivity.")
2018-09-17 22:07:00 +00:00
await voice_client.disconnect()
2018-11-06 22:11:35 +00:00
await self.change_presence(status=discord.Status.online, activity=None)
2018-09-17 22:07:00 +00:00
await self.main_channel.send("💤 Mi sono disconnesso dalla cv per inattività.")
2018-09-14 23:05:19 +00:00
2018-11-18 16:38:02 +00:00
async def create_activityreport(self):
logger.debug("Fetching Discord users...")
discord_users = list(self.main_guild.members)
online_members_count = 0
ingame_members_count = 0
cv_count = 0
cv_members_count = 0
non_empty_channels = []
for member in discord_users:
if member.bot:
continue
2018-11-19 23:07:53 +00:00
if member.voice is not None and member.voice.channel != self.main_guild.afk_channel:
2018-11-18 16:38:02 +00:00
cv_count += 1
if member.voice.channel.id not in non_empty_channels:
non_empty_channels.append(member.voice.channel.id)
if len(member.roles) >= 2:
2018-11-19 23:07:53 +00:00
if member.voice is not None and member.voice.channel != self.main_guild.afk_channel:
2018-11-18 16:38:02 +00:00
cv_members_count += 1
if member.status != discord.Status.offline and member.status != discord.Status.idle:
online_members_count += 1
if member.activity is not None and member.activity.type == discord.ActivityType.playing:
ingame_members_count += 1
logger.debug("Creating and committing db.ActivityReport...")
session = db.Session()
activityreport = db.ActivityReport(timestamp=datetime.datetime.now(),
discord_members_online=online_members_count,
discord_members_ingame=ingame_members_count,
discord_cv=cv_count,
discord_members_cv=cv_members_count,
discord_channels_used=len(non_empty_channels))
session.add(activityreport)
await loop.run_in_executor(executor, session.commit)
await loop.run_in_executor(executor, session.close)
logger.info("ActivityReport created.")
async def activity_task(self):
2018-11-18 16:42:49 +00:00
await self.wait_until_ready()
2018-12-03 23:56:29 +00:00
if self.activity_report_sample_time == math.inf:
return
2018-11-18 16:38:02 +00:00
while True:
await self.create_activityreport()
2018-12-03 23:56:29 +00:00
logger.debug(f"Waiting {self.activity_report_sample_time} seconds before the next record.")
await asyncio.sleep(self.activity_report_sample_time)
2018-11-18 16:38:02 +00:00
2018-12-04 14:21:09 +00:00
async def add_video_from_url(self, url: str, index: typing.Optional[int] = None, enqueuer: discord.Member = None):
2018-08-08 12:09:57 +00:00
# Retrieve info
2018-09-18 23:04:48 +00:00
logger.debug(f"Retrieving info for {url}.")
2018-12-06 23:44:43 +00:00
async with self.main_channel.typing():
with youtube_dl.YoutubeDL({"quiet": True,
"ignoreerrors": True,
"simulate": True}) as ytdl:
info = await loop.run_in_executor(executor,
functools.partial(ytdl.extract_info, url=url, download=False))
if info is None:
logger.debug(f"No video found at {url}.")
await self.main_channel.send(f"⚠️ Non è stato trovato nessun video all'URL `{url}`,"
f" pertanto non è stato aggiunto alla coda.")
return
if "entries" in info:
logger.debug(f"Playlist detected at {url}.")
for entry in info["entries"]:
self.video_queue.add(YoutubeDLVideo(entry["webpage_url"], enqueuer=enqueuer), index)
return
logger.debug(f"Single video detected at {url}.")
self.video_queue.add(YoutubeDLVideo(url, enqueuer=enqueuer), index)
2018-05-25 17:58:30 +00:00
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-08 12:09:57 +00:00
@command
async def null(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
pass
2018-05-25 17:58:30 +00:00
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-08 12:09:57 +00:00
@command
async def cmd_ping(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
await channel.send(f"Pong!")
2018-05-25 17:58:30 +00:00
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-08 12:09:57 +00:00
@command
async def cmd_register(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
2018-07-31 21:41:05 +00:00
session = db.Session()
2018-08-08 12:09:57 +00:00
if len(params) < 1:
await channel.send("⚠️ Non hai specificato un username!\n"
"Sintassi corretta: `!register <username_ryg>`")
2018-05-26 08:50:54 +00:00
return
2018-08-08 12:09:57 +00:00
try:
# noinspection PyTypeChecker
d = db.Discord.create(session,
royal_username=params[0],
discord_user=author)
except errors.AlreadyExistingError:
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Il tuo account Discord è già collegato a un account RYG "
2018-08-08 12:09:57 +00:00
"o l'account RYG che hai specificato è già collegato a un account Discord.")
return
session.add(d)
session.commit()
session.close()
await channel.send("✅ Sincronizzazione completata!")
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-08 12:09:57 +00:00
@command
async def cmd_cv(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
"""Summon the bot in the author's voice channel."""
if author is None:
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Questo comando richiede un autore.")
2018-08-08 12:09:57 +00:00
return
if author.voice is None:
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Non sei in nessun canale!")
2018-08-08 12:09:57 +00:00
return
if author.voice.channel == self.main_guild.afk_channel:
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Non posso connettermi al canale AFK!")
2018-08-08 12:09:57 +00:00
return
if author.voice.channel.bitrate < 64000:
await channel.send(" Sei in un canale con un bitrate ridotto.\n"
"L'utilizzo del bot in quel canale ignorerà il limite di bitrate e potrebbe causare lag"
" o eccessivo consumo di dati.\n"
"Se vuoi procedere comunque, scrivi `!yes`.")
try:
await self.wait_for("message", check=lambda m: m.content == "!yes", timeout=10.0)
except asyncio.TimeoutError:
return
# Check if there's already a connected client
for voice_client in self.voice_clients:
if voice_client.channel in self.main_guild.channels and voice_client.is_connected():
2018-09-18 23:04:48 +00:00
logger.info(f"Moving to {author.voice.channel.name}.")
2018-08-08 12:09:57 +00:00
await voice_client.move_to(author.voice.channel)
await channel.send(f"⤵️ Mi sono spostato in <#{author.voice.channel.id}>.")
break
else:
2018-09-18 23:04:48 +00:00
logger.info(f"Connecting to {author.voice.channel.name}.")
2018-08-08 12:09:57 +00:00
await author.voice.channel.connect()
await channel.send(f"⤵️ Mi sono connesso in <#{author.voice.channel.id}>.")
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-08 12:09:57 +00:00
@command
@requires_connected_voice_client
async def cmd_play(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
if len(params) < 2:
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Non hai specificato una canzone da riprodurre!\n"
2018-08-08 12:09:57 +00:00
"Sintassi: `!play <url|ricercayoutube|nomefile>`")
return
2018-12-03 23:56:29 +00:00
self.radio_messages_next_in -= 1
if self.radio_messages_next_in <= 0:
2019-01-02 19:44:10 +00:00
radio_message = random.sample(self.radio_messages, 1)[0]
2018-12-03 23:56:29 +00:00
# pycharm are you drunk
2019-01-02 19:47:20 +00:00
# noinspection PyAttributeOutsideInit
2018-12-03 23:56:29 +00:00
self.radio_messages_next_in = self.radio_messages_every
2018-11-28 14:47:08 +00:00
await self.add_video_from_url(radio_message)
await channel.send(f"📻 Aggiunto un messaggio radio, disattiva con `!radiomessages off`.")
logger.info(f"Radio message added to the queue.")
2018-08-08 12:09:57 +00:00
# Parse the parameter as URL
2018-12-04 16:55:43 +00:00
url = re.match(r"(?:https?://|ytsearch[0-9]*:|scsearch[0-9]*:).*", " ".join(params[1:]).strip("<>"))
2018-08-08 12:09:57 +00:00
if url is not None:
# This is a url
await self.add_video_from_url(url.group(0), enqueuer=author)
await channel.send(f"✅ Video aggiunto alla coda.")
2018-09-18 23:04:48 +00:00
logger.debug(f"Added {url} to the queue as URL.")
2018-08-08 12:09:57 +00:00
return
# Search the parameter on youtube
search = " ".join(params[1:])
# This is a search
await self.add_video_from_url(url=f"ytsearch:{search}", enqueuer=author)
await channel.send(f"✅ Video aggiunto alla coda.")
2018-12-04 14:21:09 +00:00
logger.debug(f"Added ytsearch:{search} to the queue.")
2018-08-08 12:09:57 +00:00
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-08 12:09:57 +00:00
@command
@requires_connected_voice_client
async def cmd_skip(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
for voice_client in self.voice_clients:
if voice_client.is_playing():
voice_client.stop()
await channel.send(f"⏩ Video saltato.")
2018-09-18 23:04:48 +00:00
logger.debug(f"A song was skipped.")
2018-08-08 12:09:57 +00:00
break
else:
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Non c'è nessun video in riproduzione.")
2018-08-01 23:11:43 +00:00
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-08 12:09:57 +00:00
@command
@requires_connected_voice_client
async def cmd_remove(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
if len(self.video_queue) == 0:
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Non c'è nessun video in coda.")
2018-08-08 12:09:57 +00:00
return
if len(params) == 1:
index = len(self.video_queue) - 1
else:
try:
index = int(params[1]) - 1
except ValueError:
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Il numero inserito non è valido.\n"
2018-08-08 12:09:57 +00:00
"Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`")
return
if len(params) < 3:
if abs(index) >= len(self.video_queue):
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Il numero inserito non corrisponde a nessun video nella playlist.\n"
2018-08-08 12:09:57 +00:00
"Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`")
return
2018-11-28 15:44:34 +00:00
video = self.video_queue.list.pop(index)
2018-08-08 12:09:57 +00:00
await channel.send(f":regional_indicator_x: {str(video)} è stato rimosso dalla coda.")
2018-09-18 23:04:48 +00:00
logger.debug(f"Removed from queue: {video.plain_text()}")
2018-08-08 12:09:57 +00:00
return
try:
start = int(params[1]) - 1
except ValueError:
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Il numero iniziale inserito non è valido.\n"
2018-08-08 12:09:57 +00:00
"Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`")
return
if start >= len(self.video_queue):
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Il numero iniziale inserito non corrisponde a nessun video nella"
2018-08-08 12:09:57 +00:00
" playlist.\n"
"Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`")
return
try:
end = int(params[2]) - 2
except ValueError:
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Il numero finale inserito non è valido.\n"
2018-08-08 12:09:57 +00:00
"Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`")
return
if end >= len(self.video_queue):
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Il numero finale inserito non corrisponde a nessun video nella"
2018-08-08 12:09:57 +00:00
" playlist.\n"
"Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`")
return
if start > end:
2018-12-04 10:18:00 +00:00
await channel.send("⚠️ Il numero iniziale è maggiore del numero finale.\n"
2018-08-08 12:09:57 +00:00
"Sintassi: `!remove [numerovideoiniziale] [numerovideofinale]`")
return
2018-11-28 15:44:34 +00:00
del self.video_queue.list[start:end]
2018-08-08 12:09:57 +00:00
await channel.send(f":regional_indicator_x: {end - start} video rimossi dalla coda.")
2018-09-18 23:04:48 +00:00
logger.debug(f"Removed from queue {end - start} videos.")
2018-08-08 12:09:57 +00:00
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-08 12:09:57 +00:00
@command
async def cmd_queue(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
2018-12-04 10:18:00 +00:00
msg = ""
2018-12-04 16:55:43 +00:00
if self.video_queue.loop_mode == LoopMode.NORMAL:
msg += "Modalità attuale: :arrow_right: **Nessuna ripetizione**\n"
elif self.video_queue.loop_mode == LoopMode.LOOP_QUEUE:
msg += "Modalità attuale: :repeat: **Ripeti intera coda**\n"
elif self.video_queue.loop_mode == LoopMode.LOOP_SINGLE:
msg += "Modalità attuale: :repeat_one: **Ripeti canzone singola**\n"
elif self.video_queue.loop_mode == LoopMode.FOLLOW_SUGGESTIONS:
msg += "Modalità attuale: :arrows_clockwise: **Continua con video suggeriti**\n"
elif self.video_queue.loop_mode == LoopMode.AUTO_SHUFFLE:
msg += "Modalità attuale: :twisted_rightwards_arrows: **Video casuale dalla coda**\n"
elif self.video_queue.loop_mode == LoopMode.LOOPING_SHUFFLE:
2018-12-06 23:31:12 +00:00
msg += "Modalità attuale: :arrows_counterclockwise: **Video casuali infiniti dalla coda**\n"
2018-12-04 10:18:00 +00:00
msg += "**Video in coda:**\n"
2018-12-04 16:55:43 +00:00
if self.video_queue.now_playing is None:
msg += ":cloud: _nessuno_"
else:
msg += f":arrow_forward: {str(self.video_queue.now_playing)}\n"
if self.video_queue.loop_mode == LoopMode.NORMAL:
2018-12-04 10:18:00 +00:00
for index, video in enumerate(self.video_queue.list[:10]):
2019-01-02 19:33:15 +00:00
msg += f"{number_emojis[index]} {str(video)}\n"
2018-12-04 10:18:00 +00:00
if len(self.video_queue) > 10:
msg += f"più altri {len(self.video_queue) - 10} video!"
2018-12-04 16:55:43 +00:00
elif self.video_queue.loop_mode == LoopMode.LOOP_QUEUE:
2018-12-04 10:18:00 +00:00
for index, video in enumerate(self.video_queue.list[:10]):
2019-01-02 19:33:15 +00:00
msg += f"{number_emojis[index]} {str(video)}\n"
2018-12-04 10:18:00 +00:00
if len(self.video_queue) > 10:
msg += f"più altri {len(self.video_queue) - 10} video che si ripetono!"
2018-12-04 16:55:43 +00:00
else:
if len(self.video_queue) < 6:
count = len(self.video_queue)
while count < 10:
for index, video in enumerate(self.video_queue.list[:10]):
msg += f":asterisk: {str(video)}\n"
count += len(self.video_queue)
msg += "e avanti così!"
elif self.video_queue.loop_mode == LoopMode.LOOP_SINGLE:
video = self.video_queue.now_playing
2018-12-04 10:18:00 +00:00
for index in range(9):
2018-12-04 16:55:43 +00:00
msg += f":asterisk: {str(video)}\n"
msg += "all'infinito!"
elif self.video_queue.loop_mode == LoopMode.FOLLOW_SUGGESTIONS:
msg += ":rainbow:"
elif self.video_queue.loop_mode == LoopMode.AUTO_SHUFFLE:
2018-12-04 10:18:00 +00:00
for index, video in enumerate(self.video_queue.list[:10]):
2018-12-04 16:55:43 +00:00
msg += f":hash: {str(video)}\n"
2018-12-04 10:18:00 +00:00
if len(self.video_queue) > 10:
msg += f"più altri {len(self.video_queue) - 10} video!"
2018-12-04 16:55:43 +00:00
elif self.video_queue.loop_mode == LoopMode.LOOPING_SHUFFLE:
2018-12-04 10:18:00 +00:00
for index, video in enumerate(self.video_queue.list[:10]):
2018-12-04 16:55:43 +00:00
msg += f":hash: {str(video)}\n"
2018-12-04 10:18:00 +00:00
if len(self.video_queue) > 10:
msg += f"più altri {len(self.video_queue) - 10} video che si ripetono!"
2018-12-04 16:55:43 +00:00
else:
if len(self.video_queue) < 6:
count = len(self.video_queue)
while count < 10:
for index, video in enumerate(self.video_queue.list[:10]):
msg += f":asterisk: {str(video)}\n"
count += len(self.video_queue)
msg += "a ripetizione casuale!"
2018-08-08 12:09:57 +00:00
await channel.send(msg)
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-08 12:09:57 +00:00
@command
@requires_connected_voice_client
async def cmd_shuffle(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
if len(self.video_queue) == 0:
await channel.send("⚠ Non ci sono video in coda!")
return
2018-09-18 23:04:48 +00:00
logger.info(f"The queue was shuffled by {author.name}#{author.discriminator}.")
2018-12-03 23:56:29 +00:00
self.video_queue.shuffle()
2019-01-02 19:47:20 +00:00
await channel.send("🔀 Shuffle completo!")
2018-08-08 12:09:57 +00:00
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-08 12:09:57 +00:00
@command
@requires_connected_voice_client
async def cmd_clear(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
if len(self.video_queue) == 0:
await channel.send("⚠ Non ci sono video in coda!")
return
2018-09-18 23:04:48 +00:00
logger.info(f"The queue was cleared by {author.name}#{author.discriminator}.")
2018-12-03 23:56:29 +00:00
self.video_queue.clear()
2018-08-08 12:09:57 +00:00
await channel.send(":regional_indicator_x: Tutti i video in coda rimossi.")
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-08 12:09:57 +00:00
@command
async def cmd_radiomessages(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
2019-01-02 19:33:15 +00:00
if not self.radio_messages:
2018-11-28 14:47:08 +00:00
await channel.send("⚠ I messaggi radio sono stati disabilitati dall'amministratore del bot.")
return
2018-08-08 12:09:57 +00:00
if len(params) < 2:
2018-11-28 14:47:08 +00:00
await channel.send("⚠ Sintassi del comando non valida.\n"
"Sintassi: `!radiomessages <on|off>`")
2018-08-08 12:09:57 +00:00
else:
if params[1].lower() == "on":
2018-11-28 14:47:08 +00:00
self.radio_messages_next_in = self.radio_messages_every
2018-08-08 12:09:57 +00:00
elif params[1].lower() == "off":
2018-12-06 23:31:12 +00:00
# noinspection PyAttributeOutsideInit wtf
2018-11-28 14:47:08 +00:00
self.radio_messages_next_in = math.inf
2018-08-08 12:09:57 +00:00
else:
await channel.send("⚠ Sintassi del comando non valida.\n"
2018-11-28 14:47:08 +00:00
"Sintassi: `!radiomessages <on|off>`")
2018-08-08 12:09:57 +00:00
return
2018-12-03 23:56:29 +00:00
logger.info(f"Radio messages status to {'enabled' if self.radio_messages_next_in < math.inf else 'disabled'}.")
2018-12-04 14:21:09 +00:00
await channel.send(
f"📻 Messaggi radio **{'attivati' if self.radio_messages_next_in < math.inf else 'disattivati'}**.")
2018-06-13 22:10:57 +00:00
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-16 17:33:38 +00:00
@command
@requires_connected_voice_client
async def cmd_pause(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
2018-08-17 13:09:03 +00:00
for voice_client in self.voice_clients:
if voice_client.is_playing():
voice_client.pause()
2018-09-18 23:04:48 +00:00
logger.debug(f"The audio stream was paused.")
2018-08-17 13:09:03 +00:00
await channel.send(f"⏸ Riproduzione messa in pausa.\n"
f"Riprendi con `!resume`.")
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-08-17 13:09:03 +00:00
@command
@requires_connected_voice_client
async def cmd_resume(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
for voice_client in self.voice_clients:
2018-10-04 10:46:15 +00:00
if voice_client.is_paused():
2018-08-17 13:09:03 +00:00
voice_client.resume()
2018-09-18 23:04:48 +00:00
logger.debug(f"The audio stream was resumed.")
2018-08-17 13:09:03 +00:00
await channel.send(f"⏯ Riproduzione ripresa.")
2018-08-16 17:33:38 +00:00
2019-01-02 19:51:43 +00:00
# noinspection PyUnusedLocal
2018-12-04 10:18:00 +00:00
@command
@requires_connected_voice_client
2018-12-06 23:31:12 +00:00
async def cmd_mode(self, channel: discord.TextChannel, author: discord.Member, params: typing.List[str]):
2018-12-04 10:18:00 +00:00
if len(params) < 2:
await channel.send("⚠ Sintassi del comando non valida.\n"
2018-12-06 23:31:12 +00:00
"Sintassi: `!mode <normal|repeat|loop|random|endless>`")
2018-12-04 16:55:43 +00:00
return
2018-12-06 23:31:12 +00:00
if params[1] == "normal":
2018-12-04 10:18:00 +00:00
self.video_queue.loop_mode = LoopMode.NORMAL
await channel.send("➡️ Modalità di coda impostata: **Nessuna ripetizione**")
2018-12-06 23:31:12 +00:00
elif params[1] == "repeat":
2018-12-04 10:18:00 +00:00
self.video_queue.loop_mode = LoopMode.LOOP_SINGLE
await channel.send("🔂 Modalità di coda impostata: **Ripeti canzone singola**")
2018-12-06 23:31:12 +00:00
elif params[1] == "loop":
2018-12-04 10:18:00 +00:00
self.video_queue.loop_mode = LoopMode.LOOP_QUEUE
await channel.send("🔁 Modalità di coda impostata: **Ripeti intera coda**")
elif params[1] == "suggest":
# self.video_queue.loop_mode = LoopMode.FOLLOW_SUGGESTIONS
await channel.send("⚠️ La modalità **Continua con video suggeriti** non è ancora stata implementata.")
2018-12-06 23:31:12 +00:00
elif params[1] == "random":
2018-12-04 10:18:00 +00:00
self.video_queue.loop_mode = LoopMode.AUTO_SHUFFLE
await channel.send("🔀 Modalità di coda impostata: **Video casuale dalla coda**")
2018-12-06 23:31:12 +00:00
elif params[1] == "endless":
2018-12-04 10:18:00 +00:00
self.video_queue.loop_mode = LoopMode.LOOPING_SHUFFLE
2018-12-06 23:31:12 +00:00
await channel.send("🔄 Modalità di coda impostata: **Video casuali infiniti dalla coda**")
2018-12-04 10:18:00 +00:00
else:
await channel.send("⚠️ Sintassi del comando non valida.\n"
"Sintassi: `!loop <off|loop1|loopall|suggest|shuffle|loopshuffle>`")
2018-08-16 17:33:38 +00:00
2018-01-15 12:42:40 +00:00
def process(users_connection=None):
2018-08-16 17:46:07 +00:00
logger.info("Initializing the bot...")
2018-08-07 23:30:43 +00:00
bot = RoyalDiscordBot()
2018-01-15 12:42:40 +00:00
if users_connection is not None:
2018-08-16 17:46:07 +00:00
logger.info("Initializing Telegram-Discord connection...")
2018-08-07 23:30:43 +00:00
asyncio.ensure_future(bot.feed_pipe(users_connection))
2018-08-16 17:46:07 +00:00
logger.info("Logging in...")
2018-11-28 14:47:08 +00:00
loop.run_until_complete(bot.login(bot.token, bot=True))
2018-08-16 17:46:07 +00:00
logger.info("Connecting...")
2018-09-17 22:07:00 +00:00
loop.run_until_complete(bot.connect())
logger.info("Now stopping...")
2018-12-08 11:31:26 +00:00
loop.run_until_complete(bot.main_channel.send(" Spegnimento bot in corso..."))
2018-09-17 22:07:00 +00:00
loop.run_until_complete(bot.logout())
2018-01-15 12:42:40 +00:00
if __name__ == "__main__":
2018-01-25 14:30:07 +00:00
process()