async def check_if_live(self): try: if not self.bot.is_ready(): await self.bot.wait_until_ready() # Give the error reporting cog some time to do what's in it's on_ready listener before it's ready to handle errors await asyncio.sleep(1) self.client.get_oauth() data = self.client.get_streams(user_logins=[twitchannel]) live = str(data) != '[]' if live: data = data[0] # Set bot's activity accordingly title = data['title'] title = title if title != '' else 'with no title' activity = Streaming( name=title, url=f'https://www.twitch.tv/{twitchannel}', platform='Twitch') # Determine if we should ping people based on our last saved stream start time stream_start = data['started_at'] with open('last_stream_start.txt', 'r') as f: last_stream_start = datetime.fromisoformat(f.read()) if last_stream_start < stream_start: with open('last_stream_start.txt', 'w') as f: f.write(str(stream_start)) await self.do_stream_ping(data) else: activity = None await self.bot.change_presence(activity=activity) except Exception: errorcog = self.bot.get_cog('ErrorReportingCog') await errorcog.on_error('anzt.twitch.loop')
async def update(self): last_url = None last_message = None async for message in self.text_channel.history(limit=10): if message.author == App.client.user: last_message = message break while App.client.loop.is_running(): url = App.config.radio_garden_url if url != last_url: if self.text_channel: async with self.text_channel.typing(): embed = Embed(description=url, color=0x42f58d, timestamp=datetime.utcnow()) embed.set_author(name="Radio Garden", url="https://radio.garden") if last_message: await last_message.edit(embed=embed) else: last_message = await self.text_channel.send( embed=embed) await App.client.change_presence(activity=Streaming( name=url, url="https://twitch.tv/slashnephy")) last_url = url await asyncio.sleep(30)
async def status(self, ctx, STATUS: str = "", ACTIVITY: str = "", arg1: str = ""): arg2 = ctx.getargs() status = None activity = None if STATUS.lower() in ["on", "online", "green"]: status = Status.online elif STATUS.lower() in ["off", "offline", "invisible", "grey"]: status = Status.invisible elif STATUS.lower() in [ "dnd", "donotdisturb", "do_not_disturb", "bittenichtstören", "red" ]: status = Status.dnd elif STATUS.lower() in ["idle", "abwesend", "orange", "yellow"]: status = Status.idle if ACTIVITY.lower() in ["playing", "spielt", "game", "play"]: activity = Game(name=arg1 + " " + arg2) elif ACTIVITY.lower() in [ "streaming", "streamt", "stream", "live", "twitch" ]: activity = Streaming(url="https://twitch.tv/" + arg1, name=arg2) elif ACTIVITY.lower() in [ "listening", "listen", "hört", "hören", "song" ]: activity = Activity(type=ActivityType.listening, name=arg1 + " " + arg2) elif ACTIVITY.lower() in ["watching", "watch", "schaut", "video"]: activity = Activity(type=ActivityType.watching, name=arg1 + " " + arg2) if status is not None or activity is not None: await ctx.bot.change_presence(status=status, activity=activity) else: await ctx.sendEmbed(title="Status ändern", color=0xff0000, inline=False, description=""" **Syntax:** /status <STATUS> [AKTIVITÄT] [ARGUMENTE] **Mögliche Status:** on/online (Online) idle (Abwesend) dnd (Bitte nicht stören) off/offline (Unsichtbar) **Mögliche Aktivitäten:** spielt <SPIEL> streamt <TWITCH-NAME> <SPIEL> hört <SONG> schaut <VIDEO> """)
async def update(self): last_program_title = None last_message = None last_login = datetime.now() async for message in self.text_channel.history(limit=10): if message.author == App.client.user: last_message = message break while App.client.loop.is_running(): program = await self.api.get_on_air() if program.title != last_program_title: if self.text_channel: async with self.text_channel.typing(): embed = Embed( title=program.title, description= f"{program.description or ''}\n{program.info}"[:500] + "...", url=program.url, color=0x00a7e9, timestamp=datetime.utcnow()) embed.set_image(url=program.banner_url) embed.set_author( name=f"{program.cast} ({program.station_name})" if program.cast else program.station_name, url=f"http://radiko.jp/#!/live/{program.station_id}" ) embed.set_footer( text= f"{program.start} - {program.end} ({program.sec // 60} 分間)", ) if last_message: await last_message.edit(embed=embed) else: last_message = await self.text_channel.send( embed=embed) await App.client.change_presence(activity=Streaming( name=f"{program.title} ({program.start} - {program.end})", url="https://twitch.tv/slashnephy")) if datetime.now() - last_login > timedelta(hours=3): await self.api.login() last_login = datetime.now() last_program_title = program.title await asyncio.sleep(30)
async def maintain_presence(self, **kwargs): if kwargs['state'] == 'setup': # read in the stream twitch_channel = self.config.get('general', 'twitch_channel') # read in the default presence kwargs['presence'] = self.rcpfx( self.config.get('general', 'presence')) # the URLs kwargs['stream_URL'] = 'https://twitch.tv/{}'.format( twitch_channel) kwargs[ 'API_URL'] = 'https://api.twitch.tv/kraken/streams/{}?client_id=6r0rm3qhbmjjq4z6vz4hez56tc9m4o'.format( twitch_channel) elif kwargs['state'] == 'run': # check if the stream is live async with ClientSession() as session: async with session.get(kwargs['API_URL']) as resp: info = await resp.json(content_type='application/json') if info['stream'] == None: # nothing is streaming - use default presence self.streaming = False await self.change_presence(activity=Game(kwargs['presence'])) else: # stream is live - use streaming presence if self.streaming is not None and self.streaming == False: await self.stream_channel.send( embed=self.stream_embed(info['stream'])) self.streaming = True await self.change_presence(activity=Streaming( name=info['stream']['channel']['status'], details=info['stream']['channel']['game'], url=kwargs['stream_URL'])) return kwargs
async def update(self): last_program_name = None last_message = None async for message in self.text_channel.history(limit=10): if message.author == App.client.user: last_message = message break while App.client.loop.is_running(): program = await AgqrProgram.get_on_air() if program.name != last_program_name: if self.text_channel: async with self.text_channel.typing(): embed = Embed(title=program.name, description=program.description, url=program.link_url or "https://www.agqr.jp", color=0xe30067, timestamp=datetime.utcnow()) embed.set_author(name=program.personality if program.personality else "文化放送", url=program.link_url or "https://www.agqr.jp") if last_message: await last_message.edit(embed=embed) else: last_message = await self.text_channel.send( embed=embed) await App.client.change_presence(activity=Streaming( name=program.name, url="https://twitch.tv/slashnephy")) last_program_name = program.name await asyncio.sleep(30)
async def handle_message(self, message): message = json.loads(str(message["data"], "utf-8")) data = message["data"] type = message["type"] nonce = message["nonce"] original_cluster = message.get("original_cluster") waiting_for = message.get("waiting_for") cluster_id = message.get("cluster_id") extras = message.get("extras", {}) if type == "IDENTIFY": # we're syncing this cluster with ourselves, and send back our clusters if original_cluster == CLUSTER_ID: if isinstance(data, int): self.clusters.add(data) else: for x in data: self.clusters.add(x) else: self.clusters.add(original_cluster) data = json.dumps({ "nonce": None, "cluster_id": CLUSTER_ID, "data": list(self.clusters), "type": "IDENTIFY", "original_cluster": original_cluster, "waiting_for": waiting_for }) await self.redis.publish( f"{RELEASE}:CLUSTER_{original_cluster}", data) elif type == "VERIFICATION": discord_id = int(data["discordID"]) guild_id = int(data["guildID"]) roblox_id = data["robloxID"] #roblox_accounts = data["robloxAccounts"] guild = Bloxlink.get_guild(guild_id) if guild: member = guild.get_member(discord_id) if not member: try: member = await guild.fetch_member(discord_id) except NotFound: return try: roblox_user, _ = await get_user(roblox_id=roblox_id) except RobloxDown: try: await member.send( "Roblox appears to be down, so I was unable to retrieve your Roblox information. Please try again later." ) except Forbidden: pass return except RobloxAPIError as e: print(e, flush=True) try: await member.send( "An unknown Roblox API error occured. Please try again later." ) except Forbidden: pass return try: added, removed, nickname, errors, warnings, roblox_user = await guild_obligations( member, guild=guild, join=True, roles=True, nickname=True, roblox_user=roblox_user, cache=False, dm=False, exceptions=("Blacklisted", "BloxlinkBypass", "RobloxAPIError", "RobloxDown", "PermissionError")) except Blacklisted as b: blacklist_text = "" if isinstance(b.message, str): blacklist_text = f"You have an active restriction for: `{b}`" else: blacklist_text = f"You have an active restriction from Bloxlink." try: await member.send( f"Failed to update you in the server: `{blacklist_text}`" ) except Forbidden: pass except BloxlinkBypass: try: await member.send( f"You have the `Bloxlink Bypass` role, so I am unable to update you in the server." ) except Forbidden: pass except RobloxAPIError: try: await member.send( "An unknown Roblox API error occured, so I was unable to update you in the server. Please try again later." ) except Forbidden: pass except RobloxDown: try: await member.send( "Roblox appears to be down, so I was unable to retrieve your Roblox information. Please try again later." ) except Forbidden: pass except PermissionError as e: try: await member.send( f"A permission error occured, so I was unable to update you in the server: `{e}`" ) except Forbidden: pass except CancelCommand: pass else: verified_dm, guild_data = await get_guild_value( guild, ["joinDM", ""], return_guild_data=True) server_message = "" if verified_dm and verified_dm != DEFAULTS.get( "welcomeMessage"): server_message = await get_nickname( member, verified_dm, guild_data=guild_data, roblox_user=roblox_user, dm=True, is_nickname=False) server_message = f"\n\nThis message was set by the Server Admins:\n{server_message}"[: 1500] try: await member.send( f"Your account was successfully updated to **{roblox_user.username}** in the server **{guild.name}.**" f"{server_message}") except Forbidden: pass await post_event( guild, guild_data, "verification", f"{member.mention} has **verified** as `{roblox_user.username}`.", GREEN_COLOR) elif type == "EVAL": """ res = (await eval(data, codeblock=False)).description data = json.dumps({ "nonce": nonce, "cluster_id": CLUSTER_ID, "data": res, "type": "CLIENT_RESULT", "original_cluster": original_cluster, "waiting_for": waiting_for }) await self.redis.publish(f"{RELEASE}:CLUSTER_{original_cluster}", data) """ pass elif type == "CLIENT_RESULT": task = self.pending_tasks.get(nonce) if task: task[1][cluster_id] = data task[2] += 1 waiting_for = message["waiting_for"] or len(self.clusters) if task[2] == waiting_for: if not task[0].done(): task[0].set_result(True) elif type == "DM": if 0 in SHARD_RANGE: try: message_ = await Bloxlink.wait_for( "message", check=lambda m: m.author.id == data and not m.guild, timeout=PROMPT["PROMPT_TIMEOUT"]) except asyncio.TimeoutError: message_ = "cancel (timeout)" data = json.dumps({ "nonce": nonce, "cluster_id": CLUSTER_ID, "data": getattr(message_, "content", message_), "type": "CLIENT_RESULT", "original_cluster": original_cluster, "waiting_for": waiting_for }) await self.redis.publish( f"{RELEASE}:CLUSTER_{original_cluster}", data) elif type == "DM_AND_INTERACTION": if 0 in SHARD_RANGE: try: task_1 = asyncio.create_task( suppress_timeout_errors( Bloxlink.wait_for( "message", check=lambda m: m.author.id == data and not m. guild, timeout=PROMPT["PROMPT_TIMEOUT"]))) task_2 = asyncio.create_task( suppress_timeout_errors( Bloxlink.wait_for( "interaction", check=lambda i: i.user.id == data and not i. guild_id and i.data.get("custom_id"), timeout=PROMPT["PROMPT_TIMEOUT"]))) result_set, pending = await asyncio.wait( {task_1, task_2}, return_when=asyncio.FIRST_COMPLETED, timeout=PROMPT["PROMPT_TIMEOUT"]) if result_set: item = next(iter(result_set)).result() if hasattr(item, "content"): message_content = { "type": "message", "content": item.content } else: if item.data["component_type"] == 3: message_content = { "type": "select", "values": item.data["values"] } else: message_content = { "type": "button", "content": item.data["custom_id"] } else: message_content = { "type": "message", "content": "cancel (timeout)" } except asyncio.TimeoutError: message_content = { "type": "message", "content": "cancel (timeout)" } data = json.dumps({ "nonce": nonce, "cluster_id": CLUSTER_ID, "data": message_content, "type": "CLIENT_RESULT", "original_cluster": original_cluster, "waiting_for": waiting_for }) await self.redis.publish( f"{RELEASE}:CLUSTER_{original_cluster}", data) elif type == "STATS": seconds = floor(time() - STARTED) m, s = divmod(seconds, 60) h, m = divmod(m, 60) d, h = divmod(h, 24) days, hours, minutes, seconds = None, None, None, None if d: days = f"{d}d" if h: hours = f"{h}h" if m: minutes = f"{m}m" if s: seconds = f"{s}s" uptime = f"{days or ''} {hours or ''} {minutes or ''} {seconds or ''}".strip( ) process = Process(getpid()) mem = floor(process.memory_info()[0] / float(2**20)) data = json.dumps({ "nonce": nonce, "cluster_id": CLUSTER_ID, "data": (len(self.client.guilds), mem, uptime), "type": "CLIENT_RESULT", "original_cluster": original_cluster, "waiting_for": waiting_for }) await self.redis.publish(f"{RELEASE}:CLUSTER_{original_cluster}", data) elif type == "USERS": data = json.dumps({ "nonce": nonce, "cluster_id": CLUSTER_ID, "data": (sum([g.member_count for g in self.client.guilds]), len(self.client.guilds)), "type": "CLIENT_RESULT", "original_cluster": original_cluster, "waiting_for": waiting_for }) await self.redis.publish(f"{RELEASE}:CLUSTER_{original_cluster}", data) elif type == "PLAYING_STATUS": presence_type = extras.get("presence_type", "normal") playing_status = extras.get("status", PLAYING_STATUS).format(prefix=PREFIX) if presence_type == "normal": await Bloxlink.change_presence(status=Status.online, activity=Game(playing_status)) elif presence_type == "streaming": stream_url = extras.get("stream_url", "https://twitch.tv/blox_link") await Bloxlink.change_presence( activity=Streaming(name=playing_status, url=stream_url)) data = json.dumps({ "nonce": nonce, "cluster_id": CLUSTER_ID, "data": True, "type": "CLIENT_RESULT", "original_cluster": original_cluster, "waiting_for": waiting_for }) await self.redis.publish(f"{RELEASE}:CLUSTER_{original_cluster}", data)
async def handle_message(self, message): message = json.loads(str(message["data"], "utf-8")) data = message["data"] type = message["type"] nonce = message["nonce"] original_cluster = message.get("original_cluster") waiting_for = message.get("waiting_for") cluster_id = message.get("cluster_id") extras = message.get("extras", {}) if type == "IDENTIFY": # we're syncing this cluster with ourselves, and send back our clusters if original_cluster == CLUSTER_ID: if isinstance(data, int): self.clusters.add(data) else: for x in data: self.clusters.add(x) else: self.clusters.add(original_cluster) data = json.dumps({ "nonce": None, "cluster_id": CLUSTER_ID, "data": list(self.clusters), "type": "IDENTIFY", "original_cluster": original_cluster, "waiting_for": waiting_for }) await self.redis.publish( f"{RELEASE}:CLUSTER_{original_cluster}", data) elif type == "VERIFICATION": discord_id = int(data["discordID"]) guild_id = int(data["guildID"]) roblox_id = data["robloxID"] #roblox_accounts = data["robloxAccounts"] guild = Bloxlink.get_guild(guild_id) if guild: member = guild.get_member(discord_id) if not member: try: member = await guild.fetch_member(discord_id) except NotFound: return roblox_user, _ = await get_user(roblox_id=roblox_id) try: added, removed, nickname, errors, roblox_user = await guild_obligations( member, guild=guild, roles=True, nickname=True, roblox_user=roblox_user, cache=False, dm=False, exceptions=("Blacklisted", "BloxlinkBypass", "RobloxAPIError", "RobloxDown", "PermissionError")) except Blacklisted as b: blacklist_text = "" if str(b): blacklist_text = f"You have an active restriction for: ``{b}``" else: blacklist_text = f"You have an active restriction from Bloxlink." try: await member.send( f"Failed to update you in the server: ``{blacklist_text}``" ) except Forbidden: pass except BloxlinkBypass: try: await member.send( f"You have the ``Bloxlink Bypass`` role, so I am unable to update you in the server." ) except Forbidden: pass except RobloxAPIError: try: await member.send( "An unknown Roblox API error occured, so I was unable to update you in the server. Please try again later." ) except Forbidden: pass except RobloxDown: try: await member.send( "Roblox appears to be down, so I was unable to retrieve your Roblox information. Please try again later." ) except Forbidden: pass except PermissionError as e: try: await member.send( f"A permission error occured, so I was unable to update you in the server: ``{e}``" ) except Forbidden: pass except CancelCommand: pass else: try: await member.send( f"Your account was successfully updated to **{roblox_user.username}** in the server **{guild.name}.**" ) except Forbidden: pass guild_data = await self.r.table("guilds").get(str( guild.id)).run() or {} # FIXME: use cache await post_event( guild, guild_data, "verification", f"{member.mention} has **verified** as ``{roblox_user.username}``.", GREEN_COLOR) elif type == "EVAL": res = (await eval(data, codeblock=False)).description data = json.dumps({ "nonce": nonce, "cluster_id": CLUSTER_ID, "data": res, "type": "CLIENT_RESULT", "original_cluster": original_cluster, "waiting_for": waiting_for }) await self.redis.publish(f"{RELEASE}:CLUSTER_{original_cluster}", data) elif type == "CLIENT_RESULT": task = self.pending_tasks.get(nonce) if task: task[1][cluster_id] = data task[2] += 1 waiting_for = message["waiting_for"] or len(self.clusters) if task[2] == waiting_for: if not task[0].done(): task[0].set_result(True) elif type == "DM": if 0 in SHARD_RANGE: try: message_ = await Bloxlink.wait_for( "message", check=lambda m: m.author.id == data and not m.guild, timeout=PROMPT["PROMPT_TIMEOUT"]) except asyncio.TimeoutError: message_ = "cancel (timeout)" data = json.dumps({ "nonce": nonce, "cluster_id": CLUSTER_ID, "data": getattr(message_, "content", message_), "type": "CLIENT_RESULT", "original_cluster": original_cluster, "waiting_for": waiting_for }) await self.redis.publish( f"{RELEASE}:CLUSTER_{original_cluster}", data) elif type == "STATS": seconds = floor(time() - STARTED) m, s = divmod(seconds, 60) h, m = divmod(m, 60) d, h = divmod(h, 24) days, hours, minutes, seconds = None, None, None, None if d: days = f"{d}d" if h: hours = f"{h}h" if m: minutes = f"{m}m" if s: seconds = f"{s}s" uptime = f"{days or ''} {hours or ''} {minutes or ''} {seconds or ''}".strip( ) process = Process(getpid()) mem = floor(process.memory_info()[0] / float(2**20)) data = json.dumps({ "nonce": nonce, "cluster_id": CLUSTER_ID, "data": (len(self.client.guilds), mem, uptime), "type": "CLIENT_RESULT", "original_cluster": original_cluster, "waiting_for": waiting_for }) await self.redis.publish(f"{RELEASE}:CLUSTER_{original_cluster}", data) elif type == "PLAYING_STATUS": presence_type = extras.get("presence_type", "normal") playing_status = extras.get("status", PLAYING_STATUS).format(prefix=PREFIX) if presence_type == "normal": await Bloxlink.change_presence(status=Status.online, activity=Game(playing_status)) elif presence_type == "streaming": stream_url = extras.get("stream_url", "https://twitch.tv/blox_link") await Bloxlink.change_presence( activity=Streaming(name=playing_status, url=stream_url)) data = json.dumps({ "nonce": nonce, "cluster_id": CLUSTER_ID, "data": True, "type": "CLIENT_RESULT", "original_cluster": original_cluster, "waiting_for": waiting_for }) await self.redis.publish(f"{RELEASE}:CLUSTER_{original_cluster}", data)
async def upd_(self, ctx): if ctx.author.id == discord_pers_id: activity = Streaming(name='Updating...', url='https://twitch.tv/mrdandycorn') await self.bot.change_presence(activity=activity) system('pm2 pull RaccoonBot')
async def auto_status(self): statuses = { 'streaming': [ 'n.help', 'on YouTube', 'Nitrotype', 'alone', #'Verified on 20/11/2020!', f'on {len(self.client.guilds)} servers', #f'with {self.users()} users', 'Buy premium 💠today!', 'n.premium' ], 'playing': [ 'n.help', 'Nitrotype', 'games', 'alone', 'with the devs!', #'Verified on 20/11/2020!', f'on {len(self.client.guilds)} servers', #f'with {self.users()} users', 'Buy premium 💠today!', 'n.premium' ], 'watching': [ 'n.help', 'Nitro Type Videos', 'YouTube', 'alone', #'Verified on 20/11/2020!', f'on {len(self.client.guilds)} servers', 'you', #f'with {self.users()} users', 'Buy premium 💠today!', 'n.premium' ], 'listening': [ 'n.help', 'songs alone', #'Verified on 20/11/2020!', f'songs on {len(self.client.guilds)} servers', 'I need gold', #f'songs with {self.users()} users', 'Buy premium 💠today!', 'n.premium' ] } status_type = choice([status for status in statuses]) if status_type == 'streaming': await self.client.change_presence(activity=Streaming( name=choice(statuses.get('streaming')), url='https://www.youtube.com/watch?v=Tt7bzxurJ1I')) elif status_type == 'playing': await self.client.change_presence( status=Status.idle, activity=Game(name=choice(statuses.get('playing')))) elif status_type == 'watching': await self.client.change_presence( status=Status.idle, activity=Activity(type=ActivityType.watching, name=choice(statuses.get('watching')))) elif status_type == 'listening': await self.client.change_presence( status=Status.idle, activity=Activity(type=ActivityType.listening, name=choice(statuses.get('listening'))))
async def on_ready(): streaming = Streaming(name='Gang Garrison Two', url='https://www.youtube.com/watch?v=VXVAfx5WQno') await bot.change_presence(activity=streaming)