我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用discord.HTTPException()。
def on_command_error(self, error, ctx): ignored = (commands.NoPrivateMessage, commands.DisabledCommand, commands.CheckFailure, commands.CommandNotFound, commands.UserInputError, discord.HTTPException) error = getattr(error, 'original', error) if isinstance(error, ignored): return if ctx.message.server: fmt = 'Channel: {0} (ID: {0.id})\nGuild: {1} (ID: {1.id})' else: fmt = 'Channel: {0} (ID: {0.id})' exc = traceback.format_exception(type(error), error, error.__traceback__, chain=False) description = '```py\n%s\n```' % ''.join(exc) time = datetime.datetime.utcnow() name = ctx.command.qualified_name author = '{0} (ID: {0.id})'.format(ctx.message.author) location = fmt.format(ctx.message.channel, ctx.message.server) message = '{0} at {1}: Called by: {2} in {3}. More info: {4}'.format(name, time, author, location, description) self.bot.logs['discord'].critical(message)
def channelinfo(self, ctx, channel : discord.Channel = None): """Gives you some channel information.""" if channel == None: channel = ctx.message.channel passed = (ctx.message.timestamp - channel.created_at).days try: channel_created_at = ("Created on {} ({} days ago!)".format(channel.created_at.strftime("%d %b %Y %H:%M"), passed)) em = discord.Embed(description="{}, here you go:".format(ctx.message.author.mention), title="Channel Info", color=0X008CFF) em.add_field(name="Channel Name", value=str(channel.name)) em.add_field(name="Channel ID", value=str(channel.id)) em.add_field(name="Channel Default", value=str(channel.is_default)) em.add_field(name="Channel Position", value=str(channel.position + 1)) em.add_field(name="Channel Topic", value=(channel.topic)) em.set_footer(text=channel_created_at) await self.bot.say(embed=em) except discord.HTTPException: channel_created_at = ("Created on {} ({} days ago!)".format(channel.created_at.strftime("%d %b %Y %H:%M"), passed)) em = discord.Embed(description="{}, here you go:".format(ctx.message.author.mention), title="Channel Info", color=0X008CFF) em.add_field(name="Channel Name", value=str(channel.name)) em.add_field(name="Channel ID", value=str(channel.id)) em.add_field(name="Channel Default", value=str(channel.is_default)) em.add_field(name="Channel Position", value=str(channel.position + 1)) em.add_field(name="Channel Topic", value="None") em.set_footer(text=channel_created_at) await self.bot.say(embed=em)
def suggest(self, ctx, *, suggestion : str): """Sends a suggestion to the owner.""" if settings.owner == "id_here": await self.bot.say("I have no owner set, cannot suggest.") return owner = discord.utils.get(self.bot.get_all_members(), id=settings.owner) author = ctx.message.author if ctx.message.channel.is_private is False: server = ctx.message.server source = "server **{}** ({})".format(server.name, server.id) else: source = "direct message" sender = "**{}** ({}) sent you a suggestion from {}:\n\n".format(author, author.id, source) message = sender + suggestion try: await self.bot.send_message(owner, message) except discord.errors.InvalidArgument: await self.bot.say("I cannot send your message, I'm unable to find" " my owner... *sigh*") except discord.errors.HTTPException: await self.bot.say("Your message is too long.") except: await self.bot.say("I'm unable to deliver your message. Sorry.") else: await self.bot.say("Your message has been sent.")
def bugreport(self, ctx, *, bug:str): """Report a bug in the bot.""" if settings.owner == "id_here": await self.bot.say("I have no owner set, cannot report the bug.") return owner = discord.utils.get(self.bot.get_all_members(), id=settings.owner) author = ctx.message.author if ctx.message.channel.is_private is False: server = ctx.message.server source = "server **{}** ({})".format(server.name, server.id) else: source = "direct message" sender = "**{0}** ({0.id}) sent you a bug report from {1}:\n\n".format(author, source) message = sender + bug try: await self.bot.send_message(owner, message) except discord.errors.InvalidArgument: await self.bot.say("I cannot send your bug report, I'm unable to find my owner... *sigh*") except discord.errors.HTTPException: await self.bot.say("Your bug report is too long.") except: await self.bot.say("I'm unable to deliver your bug report. Sorry.") else: await self.bot.say("Your bug report has been sent.")
def cat(self, ctx): """Get a random cat image.""" with ctx.typing(): try: image_url = await self.fetch_cat() except NotFound as e: await ctx.send(e) return try: fact = (await self.fetch_facts(1))[0] except NotFound as e: fact = '' embed = discord.Embed(description=fact or None) embed.set_image(url=image_url) try: await ctx.send(embed=embed) except discord.HTTPException: embed = discord.Embed(description=fact or None) embed.set_footer(text='Failed getting a cat image.') await ctx.send(embed=embed)
def _getmoji(self, msg): args = msg.content.split() if len(args) > 1: if len(args[1]) == 1: try: await msg.add_reaction(args[1]) except HTTPException: raise CommandSyntaxError(f"Non-emoji character set as spam reaction on server.") else: self.plugin_config[str(msg.guild.id)]["spam_reaction"] = args[1] await respond(msg, f"**AFFIRMATIVE. ANALYSIS: New spam reaction emoji: {args[1]}.**") await msg.remove_reaction(args[1], msg.guild.me) elif re.fullmatch("<:\w{1,32}:\d{1,20}>", args[1]): t_emoji = re.search("\d{1,20}", args[1])[0] if self.client.get_emoji(int(t_emoji)): self.plugin_config[str(msg.guild.id)]["spam_reaction"] = t_emoji.rjust(18, "0") await respond(msg, f"**AFFIRMATIVE. ANALYSIS: New spam reaction emoji: {args[1]}.**") else: raise CommandSyntaxError("Expected a single emoji as argument.") else: self.plugin_config[str(msg.guild.id)]["spam_reaction"] = False await respond(msg, f"**AFFIRMATIVE. Spam reaction disabled.**")
def clean(ctx, message): """ Cleans the previous messages :param ctx: :param message: :return: """ try: message_bucket = [] async for entry in ctx.logs_from(message.channel): if entry.author == ctx.user: message_bucket.append(entry) await ctx.delete_messages(message_bucket) await ctx.send_message(message.channel, ':sweat_drops: `Cleaned.`') except discord.Forbidden: await ctx.send_message(message.channel, '**Error**: `I do not have permissions to get channel logs`') return except discord.NotFound: await ctx.send_message(message.channel, '**Error**: `The channel you are requesting for doesnt exist.`') return except discord.HTTPException: return
def kick(ctx, message): """ Kick a member :param ctx: :param message: :return: """ if not message.mentions: await ctx.send_message(message.channel, "**Error** `Need to mention a user.`") return try: await ctx.kick(message.mentions[0]) await ctx.send_message(message.channel, "**Kicked**: `{}` from `{}` :thumbsup:" .format(message.mentions[0].name, message.server.name)) return except discord.Forbidden: await ctx.send_message(message.channel, "**Error** `I do not have 'Kick Users' Permission.`") return except discord.HTTPException: await ctx.send_message(message.channel, "**Error* `Kicking failed.`") return
def multiban(self, ctx, reason, delete_days: DeleteDays=2, *members: converters.RawMember): """ Bans multiple users. Functions similarly to d?ban. """ progress = await ctx.send(f'Banning {len(members)} member(s)...') reason = reason or 'No reason provided.' paginator = commands.Paginator(prefix='', suffix='') for member in members: try: await ctx.guild.ban(member, delete_message_days=delete_days, reason=f'(Multi-banned by {ctx.author}) {reason}') paginator.add_line(f'{ctx.green_tick} Banned {describe(member)}.') except discord.NotFound: # XXX: This code path might be unreachable, research further paginator.add_line(f"{ctx.red_tick} {describe(member)} wasn't found.") except discord.HTTPException: paginator.add_line(f'{ctx.red_tick} Failed to ban {describe(member)}. No permissions?') await progress.delete() for page in paginator.pages: await ctx.send(page)
def attentionseek(self, ctx: DogbotContext, replace_with='??'): """ Changes attention-seeking nicknames. This will change the nickname of anybody whose name starts with "!" to a name you specify. By default, they are renamed to "??". The renaming of attention-seekers is borrowed from the Discord API server. """ attention_seekers = [m for m in ctx.guild.members if m.display_name.startswith('!')] succeeded = len(attention_seekers) for seeker in attention_seekers: try: await seeker.edit(nick=replace_with) except discord.HTTPException: succeeded -= 1 failed_count = len(attention_seekers) - succeeded await ctx.send(f'Renamed {succeeded} attention seeker(s). Failed to rename {failed_count}.')
def mute(self, ctx, *, member: discord.Member): """Mute someone on voice and text chat. Usage: mute [person's name]""" or_check_perms(ctx, ['mute_members', 'manage_roles', 'manage_channels', 'manage_messages']) status = await ctx.send('Muting... ??') pg_task = self.loop.create_task(asyncio.wait_for(self.progress(status, 'Muting'), timeout=30, loop=self.loop)) try: ch_perms = discord.PermissionOverwrite(**{p: False for p in muted_perms}) for channel in ctx.guild.channels: await channel.set_permissions(member, ch_perms) await member.__redit(mute=True, deafen=None, reason='Mute command was used on user') pg_task.cancel() await status.delete(reason='Deleting progress/status message') await ctx.send('Successfully muted **%s**!' % str(member)) except (discord.Forbidden, discord.HTTPException): pg_task.cancel() await status.delete(reason='Deleting progress/status message') await ctx.send('**I don\'t have enough permissions to do that!**')
def unmute(self, ctx, *, member: discord.Member): """Unmute someone on voice and text chat. Usage: unmute [person's name]""" or_check_perms(ctx, ('mute_members', 'manage_roles', 'manage_channels', 'manage_messages')) status = await ctx.send('Unmuting... ??') pg_task = self.loop.create_task(asyncio.wait_for(self.progress(status, 'Unmuting'), timeout=30, loop=self.loop)) role_map = {r.name: r for r in member.roles} try: if 'Muted' in role_map: await member.remove_roles(role_map['Muted'], reason='Unmute command was used on user') ch_perms = discord.PermissionOverwrite(**{p: None for p in muted_perms}) for channel in ctx.guild.channels: await channel.set_permissions(member, ch_perms) await member.__redit(mute=False, deafen=None, reason='Unmute command was used on user') pg_task.cancel() await status.delete(reason='Deleting progress/status message') await ctx.send('Successfully unmuted **%s**!' % str(member)) except (discord.Forbidden, discord.HTTPException): pg_task.cancel() await status.delete(reason='Deleting progress/status message') await ctx.send('**I don\'t have enough permissions to do that!**')
def embed_from_json(self, ctx, *, js_text: str): """Send an embed from JSON. Usage: embed_from_json [json]""" echeck_perms(ctx, ('bot_owner',)) class SemiEmbed: def __init__(self, obj): self.obj = obj def to_dict(self): return self.obj try: embed_obj = json.loads(js_text) except json.decoder.JSONDecodeError: await ctx.send(':warning: **Invalid JSON data!**') else: sembed = SemiEmbed(embed_obj) try: await ctx.send(embed=sembed) except discord.HTTPException as e: if '400' in str(e): await ctx.send(':warning: **Couldn\'t send embed, check your data!**') else: raise e
def on_guild_join(self, guild): """Send the bot introduction message when invited.""" self.logger.info('New guild: ' + guild.name) if self.selfbot: return try: await self.send_message(guild.default_channel, join_msg) except discord.Forbidden: satisfied = False c_count = 0 try_channels = list(guild.channels) channel_count = len(try_channels) - 1 while not satisfied: with suppress(discord.Forbidden, discord.HTTPException): await self.send_message(try_channels[c_count], join_msg) satisfied = True if c_count > channel_count: self.logger.warning('Couldn\'t announce join to guild ' + guild.name) satisfied = True c_count += 1
def _notify_subscribers_of_streamer(self, streamer: Streamer): subscribers = await self.bot.database.get_subscribers_from_streamer(streamer.db_id) for (subscriber_id,) in subscribers: if subscriber_id in self.disabled_users: continue subscriber = await self._get_subscriber(subscriber_id) if subscriber: notification_embed = streamer.create_notification_embed() try: await subscriber.send(embed=notification_embed) log.info('Notified %s that streamer %s is online on %s', subscriber, streamer.channel_name, streamer.service_name) except discord.Forbidden as e: log.exception('_notify_subscribers_of_streamer: No permissions to send the message.\n%s', e) except discord.HTTPException as e: log.exception('_notify_subscribers_of_streamer: Sending the message failed.\n%s', e) except Exception as e: log.exception('_notify_subscribers_of_streamer: General exception.\n%s', e) else: log.error('_notify_subscribers_of_streamer: Subscriber not found: %s', subscriber_id)
def run(self): self._interaction = asyncio.ensure_future(self._game_screen.interact(timeout=None, delete_after=False)) self._runner = asyncio.ensure_future(self.run_loop()) # await self._game_screen.wait_until_ready() try: return await self._runner finally: # For some reason having all these games hanging around causes lag. # Until I properly make a delete_after on the paginator I'll have to # settle with this hack. async def task(): await asyncio.sleep(30) with contextlib.suppress(discord.HTTPException): await self._game_screen._message.delete() self.ctx.bot.loop.create_task(task()) self._interaction.cancel()
def join(self, invite_url: discord.Invite=None): """Joins new server""" if hasattr(self.bot.user, 'bot') and self.bot.user.bot is True: # Check to ensure they're using updated discord.py msg = ("I have a **BOT** tag, so I must be invited with an OAuth2" " link:\nFor more information: " "https://twentysix26.github.io/" "Red-Docs/red_guide_bot_accounts/#bot-invites") await self.bot.say(msg) if hasattr(self.bot, 'oauth_url'): await self.bot.whisper("Here's my OAUTH2 link:\n{}".format( self.bot.oauth_url)) return if invite_url is None: await self.bot.say("I need a Discord Invite link for the " "server you want me to join.") return try: await self.bot.accept_invite(invite_url) await self.bot.say("Server joined.") log.debug("We just joined {}".format(invite_url)) except discord.NotFound: await self.bot.say("The invite was invalid or expired.") except discord.HTTPException: await self.bot.say("I wasn't able to accept the invite." " Try again.")
def deletenewschannel(self, ctx, channel: discord.Channel): """Removes news functionality for a channel""" server = ctx.message.server if server.id not in self.settings: await self.bot.say("Nothing available for this server!") return if channel.id not in self.settings[server.id]: await self.bot.say("News functionality isn't set up for that channel!") return role = [r for r in ctx.message.server.roles if r.id == self.settings[server.id][channel.id]["role_id"]][0] try: await self.bot.delete_role(server, role) except discord.Forbidden: await self.bot.say("I cannot delete roles!") return except discord.HTTPException: await self.bot.say("Something went wrong!") return else: await self.bot.say("Role removed!") self.settings[server.id].pop(channel.id, None) dataIO.save_json("data/newsannouncer/settings.json", self.settings)
def joinnews(self, ctx): """Joins the news role for the current channel""" server = ctx.message.server channel = ctx.message.channel if server.id not in self.settings or\ channel.id not in self.settings[server.id]: await self.bot.say("No news role available here!") return author = ctx.message.author if author.id in self.settings[server.id][channel.id]["joined"]: await self.bot.say("You already have the role for this channel!") return role_id = self.settings[server.id][channel.id]["role_id"] role_to_add = [r for r in server.roles if r.id == role_id][0] try: await self.bot.add_roles(author, role_to_add) except discord.Forbidden: await self.bot.say("I don't have permissions to add roles here!") return except discord.HTTPException: await self.bot.say("Something went wrong while doing that.") return await self.bot.say("Added that role successfully") self.settings[server.id][channel.id]["joined"].append(author.id) dataIO.save_json("data/newsannouncer/settings.json", self.settings)
def unban(self, ctx, member_id: int): """Used to unban a member from this server Due to the fact that I cannot find a user without being in a server with them only the ID should be provided EXAMPLE: !unban 353217589321750912 RESULT: That dude be unbanned""" # Lets only accept an int for this method, in order to ensure only an ID is provided # Due to that though, we need to ensure a string is passed as the member's ID member = discord.Object(id=str(member_id)) try: await self.bot.unban(ctx.message.server, member) await self.bot.say("\N{OK HAND SIGN}") except discord.Forbidden: await self.bot.say("But I can't, muh permissions >:c") except discord.HTTPException: await self.bot.say("Sorry, I failed to unban that user!")
def urban(self, ctx, *, msg: str): """Pulls the top urbandictionary.com definition for a term EXAMPLE: !urban a normal phrase RESULT: Probably something lewd; this is urban dictionary we're talking about""" url = "http://api.urbandictionary.com/v0/define" params = {"term": msg} try: data = await utils.request(url, payload=params) if data is None: await self.bot.send_message(ctx.message.channel, "Sorry but I failed to connect to urban dictionary!") return # List is the list of definitions found, if it's empty then nothing was found if len(data['list']) == 0: await self.bot.say("No result with that term!") # If the list is not empty, use the first result and print it's defintion else: await self.bot.say(data['list'][0]['definition']) # Urban dictionary has some long definitions, some might not be able to be sent except discord.HTTPException: await self.bot.say('```\nError: Definition is too long for me to send```') except KeyError: await self.bot.say("Sorry but I failed to connect to urban dictionary!")
def kick(self, *, member : discord.Member): """Kicks a member from the server. In order for this to work, the bot must have Kick Member permissions. To use this command you must have Kick Members permission or have the Bot Admin role. """ try: await self.bot.kick(member) except discord.Forbidden: await self.bot.say('The bot does not have permissions to kick members.') except discord.HTTPException: await self.bot.say('Kicking failed.') else: await self.bot.say('\U0001f44c')
def ban(self, *, member : discord.Member): """Bans a member from the server. In order for this to work, the bot must have Ban Member permissions. To use this command you must have Ban Members permission or have the Bot Admin role. """ try: await self.bot.ban(member) except discord.Forbidden: await self.bot.say('The bot does not have permissions to ban members.') except discord.HTTPException: await self.bot.say('Banning failed.') else: await self.bot.say('\U0001f44c')
def softban(self, *, member : discord.Member): """Soft bans a member from the server. A softban is basically banning the member from the server but then unbanning the member as well. This allows you to essentially kick the member while removing their messages. To use this command you must have Ban Members permissions or have the Bot Admin role. Note that the bot must have the permission as well. """ try: await self.bot.ban(member) await self.bot.unban(member.server, member) except discord.Forbidden: await self.bot.say('The bot does not have permissions to ban members.') except discord.HTTPException: await self.bot.say('Banning failed.') else: await self.bot.say('\U0001f44c')
def feeds_delete(self, ctx, *, feed : str): """Removes a feed from the channel. This will also delete the associated role so this action is irreversible. """ channel = ctx.message.channel server = channel.server feeds = self.feeds.get(channel.id, {}) feed = feed.lower() if feed not in feeds: await self.bot.say('This feed does not exist.') return role = feeds.pop(feed) try: await self.bot.delete_role(server, discord.Object(id=role)) except discord.HTTPException: await self.bot.say('\U0001F52B') else: await self.feeds.put(channel.id, feeds) await self.bot.say('\U0001F6AE')
def leave(self, ctx, guild=None): """ Leaves a specified guild """ guild_names = list("{} - ID: {}".format(g.name, g.id) for g in self.bot.guilds) if guild is None: guild = await reaction_menu.start_reaction_menu( self.bot, guild_names, ctx.author, ctx.channel, count=1, timeout=60, per_page=10, header=header, return_from=self.bot.guilds, allow_none=True) guild = guild[0] else: guild = discord.utils.find(lambda s: s.name == guild or str(s.id) == guild, self.bot.guilds) if guild is None: await ctx.send("Unable to locate guild") return try: await guild.leave() await ctx.send("`Successfully left the guild`") except discord.HTTPException: await ctx.send("`Leaving the guild failed!`")
def compare_bots_users(self, guild): b = 0 u = 0 for m in guild.members: if m.bot: b += 1 else: u += 1 self.bot.logger.debug(f"{guild} [{guild.id}] Evaluated bot to user ratio for guild - Users: {u} Bots: {b}") if (b / 2) > u: self.bot.logger.debug(f"{guild} [{guild.id}] ratio too high, attempting to leave") try: await guild.leave() self.bot.logger.debug(f"{guild} [{guild.id}] left guild successfully") return 0 # left except discord.HTTPException: self.bot.logger.debug(f"{guild} [{guild.id}] failed leaving guild") return 1 # error else: self.bot.logger.debug(f"{guild} [{guild.id}] Ratio OK, not leaving guild") return 2 # nothing
def edit_mod_entry(self, modcfg, data): """Edit a moderation entry.""" modlog = data['guild'].get_channel(modcfg['mod_log_id']) if modlog is None: raise self.SayException('Moderation channel not found') try: action_data = self.cache[data['action_id']] except KeyError: raise self.SayException("Can't find action ID in cache, sorry :c") old_data = action_data['data'] old_data['reason'] = data['reason'] try: message = await modlog.get_message(action_data['message_id']) except discord.NotFound: raise self.SayException('Message to edit not found') except discord.Forbidden: raise self.SayException("Can't read messages") except discord.HTTPException as err: raise self.SayException(f'fug `{err!r}`') await message.edit(content=self.modlog_fmt(old_data))
def star(self, ctx, message_id: int): """Star a message.""" try: message = await ctx.channel.get_message(message_id) except discord.NotFound: return await ctx.send('Message not found') except discord.Forbidden: return await ctx.send("Can't retrieve message") except discord.HTTPException as err: return await ctx.send(f'Failed to retrieve message: {err!r}') try: await self.add_star(message, ctx.author) await ctx.ok() except (StarAddError, StarError) as err: log.warning(f'[star_command] Errored: {err!r}') return await ctx.send(f'Failed to add star: {err!r}')
def starrers(self, ctx, message_id: int): """Get the list of starrers from a message in the current channel.""" try: message = await ctx.channel.get_message(message_id) except discord.NotFound: return await ctx.send('Message not found') except discord.Forbidden: return await ctx.send("Can't retrieve message") except discord.HTTPException as err: return await ctx.send(f'Failed to retrieve message: {err!r}') guild = ctx.guild await self._get_starconfig(guild.id) star = await self.get_star(guild.id, message.id) if star is None: return await ctx.send('Star object not found') _, em = make_star_embed(star, message) starrers = [guild.get_member(starrer_id) for starrer_id in star['starrers']] em.add_field(name='Starrers', value=', '.join([m.display_name for m in starrers])) await ctx.send(embed=em)
def on_member_join(self, member): guild = member.guild if guild.id in WHITELIST: return bots, humans, ratio = self.bot_human_ratio(guild) bot_ratio = await self.guild_ratio(guild) if ratio > bot_ratio: log.info(f'[bh:leave:member_join] leaving {guild!r} {guild.id},' f' {ratio} ({len(bots)} / {len(humans)}) > {bot_ratio}') try: await guild.owner.send('Your guild was classified as a bot' 'collection, josé automatically left.' f'{len(bots)} bots, {len(humans)} humans, ' f'{ratio}b/h, ratio is over {BOT_RATIO_MAX}') except discord.HTTPException: pass await guild.leave()
def check_reactions_clear(self, message, reactions): """Checks for reactions clears""" #pylint: disable=unused-argument #pylint: disable=too-many-nested-blocks if message.author.id not in self.bot.blacklist: for com in self.communications: for msg in self.communications[com].messages: if msg["id"] == message.id: msg["reactions"] = {} index = -1 for i in range(0, len(msg["embed"].fields)): if msg["embed"].fields[i].name == "Reactions": index = i break if index != -1: msg["embed"].remove_field(index) for message_sent in msg["messages"]: try: await self.bot.edit_message(message_sent, embed=msg["embed"]) except discord.HTTPException: pass
def set_avatar(self, avatar_link : str): """Sets bot's avatar Parameters: avatar_link: The link of the the picture which will become the new bot's avatar Example: [p]set_avatar http://i.imgur.com/bjmbH1e.png""" r = requests.get(avatar_link) if r.status_code == 200: try: await self.bot.edit_profile(avatar=r.content) await self.bot.say("Done!") except discord.HTTPException: await self.bot.say("HTTP Exception") except discord.InvalidArgument: await self.bot.say("Wrong image format") except requests.exceptions.MissingSchema: await self.bot.say("Invalid URL") else: await self.bot.say("Error " + str(r.status_code) + ": The link must be incorrect, " \ + "make sure the link finishes with `.png`, `.jpg`, `.jpeg`, etc")
def send_log(self, server: discord.Server, log: Log): """Sends a embed corresponding to the log in the log channel of the server""" if server.id in self.servers_config["servers"]: if "log channel" in self.servers_config["servers"][server.id]: embed = log.get_embed(self.bot) channel = self.servers_config["servers"][server.id]["log channel"] try: await self.bot.send_message(destination=channel, embed=embed) except discord.Forbidden: await self.bot.send_message(destination=server.owner, content=\ "I'm not allowed to send embeds in the log channel (#" + \ channel.name + "). Please change my permissions.") except discord.NotFound: await self.bot.send_message(destination=server.owner, content=\ "I'm not allowed to send embeds in the log channel because " + \ "it doesn't exists anymore. Please set another log channel " + \ "using the `[p]set_log_channel` command.") except discord.HTTPException: pass except discord.InvalidArgument: pass
def check_new_comers(self, member): """Checks if a new comer is in the b1nzy banlist""" if member.server.id in self.b1nzy_banlist: if member.id in self.b1nzy_banlist[member.server.id]: try: await self.bot.ban(member) self.b1nzy_banlist[member.server.id].remove(member.id) if not self.b1nzy_banlist[member.server.id]: del self.b1nzy_banlist[member.server.id] self.save_b1nzy_banlist() except discord.Forbidden: await self.bot.send_message(member.server.owner, \ "Couldn't ban " + member.name + "#" + member.discriminator + \ " (" + member.id + ") who's in the b1nzy banlist --> missing permissions") except discord.HTTPException: pass
def say_victory_message(self, msg_winner, msg_no_winner): serverlist = list(self.bot.servers) for s in serverlist: if self.get_victory_messages(s): try: msg = msg_winner if self.get_show_result(s) else msg_no_winner matches_channel = self.bot.get_channel(self.get_matches_channel(s)) if matches_channel: try: await self.bot.send_message(matches_channel, msg) except (discord.Forbidden, discord.NotFound, discord.InvalidArgument): pass except discord.HTTPException: pass #except Exception as e: # print("Unable to announce end of match: %s" % e)
def contact(self, ctx, *, message): """Sends a message to the bot owner (60s cooldown). Is not affected by Octarine Core, Refresher Orb, Rearm, or cooldown reduction talents.""" try: owner = await self.bot.get_owner() except discord.NotFound: await self.bot.say("Alas, I know not who my owner is.") return author = ctx.message.author emb = discord.Embed(description = message) emb.set_author(name = "Message from %s" % author) try: await self.bot.send_message(owner, embed = emb) except discord.InvalidArgument: await self.bot.say("Alas, I know not where my owner is.") except discord.HTTPException: await self.bot.say("Alas, I could not deliver your message. Perhaps it is too long?") except: await self.bot.say("Alas, for reasons yet unknown to me, I could not deliver your message.") else: await self.bot.say("I have delivered your message with utmost haste! I pray it should arrive safely.")
def kick(self, ctx, member: str, *, reason: str=None): """Kick a Member.""" member = getUser(ctx, member) if member: try: await ctx.guild.kick(member, reason=reason) except discord.Forbidden: await edit(ctx, content="\N{HEAVY EXCLAMATION MARK SYMBOL} Missing permissions to kick this Member", ttl=5) except discord.HTTPException: await edit(ctx, content="\N{HEAVY EXCLAMATION MARK SYMBOL} Something went wrong while trying to kick...", ttl=5) else: e = discord.Embed(color=embedColor(self)) e.set_author(icon_url="https://cdn.discordapp.com/attachments/278603491520544768/301084579660300289/301063051296374794.png", name="Kicked: " + str(member)) await edit(ctx, embed=e) # Ban a Member
def ban(self, ctx, member: str, *, reason: str=None): """Ban a Member.""" member = getUser(ctx, member) if member: try: await ctx.guild.ban(member, reason=reason) except discord.Forbidden: await edit(ctx, content="\N{HEAVY EXCLAMATION MARK SYMBOL} Missing permissions to ban this Member", ttl=5) except discord.HTTPException: await edit(ctx, content="\N{HEAVY EXCLAMATION MARK SYMBOL} Something went wrong while trying to ban...", ttl=5) else: e = discord.Embed(color=embedColor(self)) e.set_author(icon_url="https://cdn.discordapp.com/attachments/278603491520544768/301087009408024580/273910007857414147.png", name="Banned: " + str(member)) await edit(ctx, embed=e) # SoftBan a Member (ban, delelte messagea and unban)
def softban(self, ctx, member: str, *, reason: str=None): """Softban a Member(Kick and delete Messages)""" member = getUser(ctx, member) if member: try: await ctx.guild.ban(member, reason=reason) await ctx.guild.unban(member) except discord.Forbidden: await edit(ctx, content="\N{HEAVY EXCLAMATION MARK SYMBOL} Missing permissions to ban this Member", ttl=5) except discord.HTTPException: await edit(ctx, content="\N{HEAVY EXCLAMATION MARK SYMBOL} Something went wrong while trying to ban...", ttl=5) else: e = discord.Embed(color=embedColor(self)) e.set_author(icon_url="https://cdn.discordapp.com/attachments/278603491520544768/301087009408024580/273910007857414147.png", name="Soft Banned: " + str(member)) await edit(ctx, embed=e)
def _colour(self, ctx, role: str, colour: str): """Set the Color of a Role.""" role = getRole(ctx, role) colour = getColor(colour) if not role: return await edit(ctx, content="\N{HEAVY EXCLAMATION MARK SYMBOL} Role not found", ttl=5) elif not colour: return await edit(ctx, content="\N{HEAVY EXCLAMATION MARK SYMBOL} Colour not found", ttl=5) else: value = discord.Colour(int((colour.hex_l.strip('#')), 16)) try: await role.edit(colour=value) except discord.HTTPException: await edit(ctx, content="\N{HEAVY EXCLAMATION MARK SYMBOL} Missing permissions to edit this role", ttl=5) else: e = discord.Embed(color=value) e.set_author(name="Changed Role Color of: " + str(role)) await edit(ctx, embed=e)
def raid_off(self, ctx): """Disables raid mode on the server. When disabled, the server verification levels are set back to Low levels and the bot will stop broadcasting join messages. """ try: await ctx.guild.edit(verification_level=discord.VerificationLevel.low) except discord.HTTPException: await ctx.send('\N{WARNING SIGN} Could not set verification level.') query = """INSERT INTO guild_mod_config (id, raid_mode, broadcast_channel) VALUES ($1, $2, NULL) ON CONFLICT (id) DO UPDATE SET raid_mode = EXCLUDED.raid_mode, broadcast_channel = NULL; """ await ctx.db.execute(query, ctx.guild.id, RaidMode.off.value) self._recently_kicked.pop(ctx.guild.id, None) await ctx.send('Raid mode disabled. No longer broadcasting join messages.')
def nostalgia(self, ctx, date: date, *, channel: discord.TextChannel = None): """Pins an old message from a specific date. If a channel is not given, then pins from the channel the command was ran on. The format of the date must be either YYYY-MM-DD or YYYY/MM/DD. """ channel = channel or ctx.channel message = await channel.history(after=date, limit=1).flatten() if len(message) == 0: return await ctx.send('Could not find message.') message = message[0] try: await message.pin() except discord.HTTPException: await ctx.send('Could not pin message.') else: await ctx.send('Pinned message.')
def feeds_delete(self, ctx, *, feed: str): """Removes a feed from the channel. This will also delete the associated role so this action is irreversible. """ query = 'DELETE FROM feeds WHERE channel_id=$1 AND name=$2 RETURNING *;' records = await ctx.db.fetch(query, ctx.channel.id, feed) self.get_feeds.invalidate(self, ctx.channel.id) if len(records) == 0: return await ctx.send('This feed does not exist.') for record in records: role = discord.utils.find(lambda r: r.id == record['role_id'], ctx.guild.roles) if role is not None: try: await role.delete() except discord.HTTPException: continue await ctx.send(f'{ctx.tick(True)} Removed feed.')
def _massmove(self, ctx, from_channel, to_channel): """Internal function: Massmove users to another voice channel""" # check if channels are voice channels. Or moving will be very... interesting... type_from = str(from_channel.type) type_to = str(to_channel.type) if type_from == 'text': await self.bot.say('{} is not a valid voice channel'.format(from_channel.name)) log.debug('SID: {}, from_channel not a voice channel'.format(from_channel.server.id)) elif type_to == 'text': await self.bot.say('{} is not a valid voice channel'.format(to_channel.name)) log.debug('SID: {}, to_channel not a voice channel'.format(to_channel.server.id)) else: try: log.debug('Starting move on SID: {}'.format(from_channel.server.id)) log.debug('Getting copy of current list to move') voice_list = list(from_channel.voice_members) for member in voice_list: await self.bot.move_member(member, to_channel) log.debug('Member {} moved to channel {}'.format(member.id, to_channel.id)) await asyncio.sleep(0.05) except discord.Forbidden: await self.bot.say('I have no permission to move members.') except discord.HTTPException: await self.bot.say('A error occured. Please try again')
def _mdm(self, ctx: commands.Context, role: discord.Role, *, message: str): """Sends a DM to all Members with the given Role. Allows for the following customizations: {0} is the member being messaged. {1} is the role they are being message through. {2} is the person sending the message. """ server = ctx.message.server sender = ctx.message.author try: await self.bot.delete_message(ctx.message) except: pass dm_these = self._get_users_with_role(server, role) for user in dm_these: try: await self.bot.send_message(user, message.format(user, role, sender)) except (discord.Forbidden, discord.HTTPException): continue
def run(self, token): """ Override's discord.py's function for running the bot """ try: super().run(token, bot=(not self.config.selfbot)) except discord.LoginFailure: log.critical("Incorrect login token") if not self.config.selfbot: log.critical( "Using your own token? Change 'selfbot' to 'True' in the config") else: log.critical( "Using an OAuth account? Change 'selfbot' to 'False' in the config") except discord.HTTPException as e: log.critical(e)
def edit_message(self, message, content, *, delete=0): """ Overrides discord.py's function for editing a message """ msg = None try: msg = await super().edit_message(message, content) log.debug( 'Edited message ID {} in #{}'.format(msg.id, msg.channel)) if msg and delete and self.config.delete: asyncio.ensure_future(self._delete_after(msg, delete)) except discord.Forbidden: log.warning( "No permission to edit a message in #{}".format(message.channel)) except discord.NotFound: log.warning( "Could not find message ID {} to edit".format(message.id)) except discord.HTTPException as e: log.warning( "Problem editing a message in #{}: {}".format(message.channel, e)) return msg