我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用discord.Channel()。
def play(self, ctx, voice_channel: discord.Channel=None): """Play Radio Haru""" server = ctx.message.server author = ctx.message.author if voice_channel == None: voice_channel = author.voice_channel if self.voice_connected(server): await self.bot.say("Already connected to a voice channel, use `{}radioharu stop` to change radio.".format(ctx.prefix)) else: try: voice = await self.bot.join_voice_channel(voice_channel) Channel = ctx.message.channel await self.bot.send_typing(Channel) player = voice.create_ffmpeg_player('https://cdn.discordapp.com/attachments/336598653923753987/360413654224601089/Radio-Haru.ogg', use_avconv=self.use_avconv) player.start() await asyncio.sleep(7) player.stop() player = voice.create_ffmpeg_player('https://stream.radioharu.pw/radioharu', use_avconv=self.use_avconv) player.start() await self.bot.say(":green_heart: **Playing Radio Haru!**") except InvalidArgument: await self.bot.say("You either didn't enter a voice channel to connect to, or weren't in one!")
def setchatchannel(self, ctx, *, channel : discord.Channel = None): """Sets the channel for bot chatter.""" isAdmin = ctx.message.author.permissions_in(ctx.message.channel).administrator # Only allow admins to change server stats if not isAdmin: await self.bot.send_message(ctx.message.channel, 'You do not have sufficient privileges to access this command.') return if channel == None: self.settings.setServerStat(ctx.message.server, "ChatChannel", "") msg = 'Chat channel removed - must use the `{}chat [message]` command to chat.'.format(ctx.prefix) await self.bot.send_message(ctx.message.channel, msg) return # If we made it this far - then we can add it self.settings.setServerStat(ctx.message.server, "ChatChannel", channel.id) msg = 'Chat channel set to **{}**.'.format(channel.name) await self.bot.send_message(ctx.message.channel, msg)
def new_channel(self, channel, guild=None): """Creates a new Dwarf ?Guild? object and connects it to the database. Parameters ---------- channel Can be a Discord ?Channel? object or a channel ID. guild : Optional Can be a Discord ?Server? object or a guild ID. Is not an optional parameter if ?channel? is not a Discord ?Channel? object. """ if isinstance(channel, discord.Channel): return Channel(id=channel.id, guild=channel.server.id) else: if guild is None: raise ValueError("Either a Channel object or both channel ID " "and guild ID must be given as argument(s).") return Channel(id=channel, guild=guild)
def __init__(self, channel, title, description, modulename="Modis", colour=0xAAFF00, thumbnail=None, image=None, datapacks=()): """Initialises variables and builds GUI Args: channel (discord.Channel): Channel to lock UI to title (str): GUI title, in bold description (str): GUI description modulename (str): Name of your module, default "Modis" colour (int): Colour of line on left, default 0xAAFF00 thumbnail (str): URL to picture shown in top right corner, default None datapacks (list): Contains tuples of (title str, data str, inline bool) """ self.channel = channel self.title = title self.description = description self.modulename = modulename self.colour = colour self.thumbnail = thumbnail self.image = image self.datapacks = datapacks self.built_embed = self.build() self.sent_embed = None
def movehere(self, channel): """ Moves the embed message to a new channel; can also be used to move the musicplayer to the front Args: channel (discord.Channel): The channel to move to """ self.logger.debug("movehere command") # Delete the old message await self.embed.delete() # Set the channel to this channel self.embed.channel = channel # Send a new embed to the channel await self.embed.send() # Re-add the reactions await self.add_reactions() self.statuslog.info("Moved to front")
def msetup(self, text_channel): """Creates the gui Args: text_channel (discord.Channel): The channel for the embed ui to run in """ if self.mready: logger.warning("Attempt to init music when already initialised") return if self.state != 'starting': logger.error("Attempt to init from wrong state ('{}'), must be 'starting'.".format(self.state)) return self.logger.debug("Setting up gui") # Create gui self.mchannel = text_channel self.new_embed_ui() await self.embed.send() await self.embed.usend() await self.add_reactions() self.mready = True
def modify_module(channel, module_name, module_state): """ Creates an embed UI containing the module modified message Args: channel (discord.Channel): The Discord channel to bind the embed to module_name (str): The name of the module that was updated module_state (bool): The current state of the module Returns: embed: The created embed """ # Create embed UI object gui = ui_embed.UI( channel, "{} updated".format(module_name), "{} is now {}".format(module_name, "activated" if module_state else "deactivated"), modulename=modulename ) return gui
def warning_max_changed(channel, max_warnings): """ Creates an embed UI containing an error message Args: channel (discord.Channel): The Discord channel to bind the embed to max_warnings (int): The new maximum warnings Returns: ui (ui_embed.UI): The embed UI object """ # Create embed UI object gui = ui_embed.UI( channel, "Maximum Warnings Changed", "Users must now have {} warnings to be banned " "(this won't ban existing users with warnings)".format(max_warnings), modulename=modulename ) return gui
def error(channel, title, description): """ Creates an embed UI containing an error message Args: channel (discord.Channel): The Discord channel to bind the embed to title (str): The title of the embed description (str): The description for the error Returns: ui (ui_embed.UI): The embed UI object """ # Create embed UI object gui = ui_embed.UI( channel, title, description, modulename=modulename ) return gui
def join(self, *, channel: discord.Channel): """Joins a voice channel.""" try: await self.bot.create_voice_client(channel) except discord.InvalidArgument: out = await self.bot.say('This is not a voice channel...') await asyncio.sleep(5) try: await self.bot.delete_messages([ctx.message, out]) except: pass except discord.ClientException: out = await self.bot.say('Already in a voice channel...') await asyncio.sleep(5) try: await self.bot.delete_messages([ctx.message, out]) except: pass else: out = await self.bot.say('Ready to play audio in ' + channel.name) await asyncio.sleep(5) try: await self.bot.delete_messages([ctx.message, out]) except: pass
def get_interface(self, channel: discord.Channel, generate=True): """ Retrieves a channel interface. If none found for the channel, a new one is created with its default values (if generate is True). :param channel: The channel the interface belongs to. :type channel: discord.Channel :param generate: Whether the interface should be auto-generated if none is found or not. :type generate: bool :return: The interface belonging to the channel, new or old. """ if channel not in self._interfaces: if not generate: return None self._interfaces[channel] = ChannelTimerInterface(channel) return self._interfaces[channel]
def spoof(self, member: discord.Member, channel: discord.Channel): """ Spoofs a channel ID if there's a set channel to spoof from the one where the command was executed. Also checks for the author's permissions to see if they can spoof commands. :param member: The member trying to spoof the command. :type member: discord.Member :param channel: The channel from which the command to spoof was sent. :type channel: discord.Channel :return: If there's a registered ID to spoof and the author has elevated permissions, returns the spoofed ID. If not, returns the same ID as channel_id """ if self.has_permission(member) and self.get_interface(channel).spoofed\ is not None: return self._interfaces[channel].spoofed return channel
def _generate_messages(self, channel: discord.Channel): """ Generates and pins the messages for the given channel. :param channel: The channel in which the messages will be generated and pinned. :type channel: discord.Channel :raises: discord.errors.Forbidden: if the client doesn't have permissions to pin messages. """ interface = self.get_interface(channel) if interface.timer is None: return interface.time_message = await self.send_message( channel, "Generating status...") interface.list_message = await self.send_message( channel, interface.timer.list_periods()) # The last message pinned ends up in the top await self.pin_message(interface.time_message) await self.pin_message(interface.list_message)
def mark_active(self, channel: discord.Channel, author: discord.Member, time: datetime): """ Marks a user as active within a channel, giving them a last-active-at timestamp. :param channel: The channel in which to mark the user as active at. :type channel: discord.Channel :param author: The user to mark as active. :type author: discord.Member :param time: The last-active-at time. :type time: datetime """ interface = self.get_interface(channel, generate=False) if interface is not None and author in interface.subbed: interface.subbed[author]['last'] = time
def channel_cmd(self, ctx, *, channel : discord.Channel = None): """Ignores a specific channel from being processed. If no channel is specified, the current channel is ignored. If a channel is ignored then the bot does not process commands in that channel until it is unignored. """ if channel is None: channel = ctx.message.channel ignored = self.config.get('ignored', []) if channel.id in ignored: await self.bot.responses.failure(message='That channel is already ignored.') return ignored.append(channel.id) await self.config.put('ignored', ignored) await self.bot.responses.success(message='Channel <#{}> will be ignored.'.format(channel.id))
def _list(self, ctx, channel: discord.Channel=None): """Lists regexes used to filter messages. Channel listing includes global patterns.""" server = ctx.message.server self.regexen = dataIO.load_json(JSON_PATH) if not self._re_present(server): await self.bot.say('There are no filter patterns set for this server.') return table = ' | '.join(['mode', 'pattern']) + '\n' # header for c in self.regexen[server.id]: if c == ALL_CHANNELS and self._re_present(server): table += '\nServer-wide:\n' elif (channel and channel.id == c) or not channel: if channel: ch_obj = channel else: ch_obj = self.bot.get_channel(c) if ch_obj is None: table += '\n' + 'Channel ID %s (deleted):' % c + '\n' if self._re_present(ch_obj): table += '\n#' + ch_obj.name + '\n' for regex, mode in self.regexen[server.id][c].items(): table += ' | '.join([mode, regex]) + '\n' await self.bot.say('```py\n' + table + '```')
def fetch_channel(self, ctx, subfolder: str, channel: discord.Channel = None, attachments: bool = None): "Fetch complete logs for a channel. Defaults to the current one." msg = await self.bot.say('Dispatching fetch task...') start = datetime.now() cookie = FetchCookie(ctx, start, msg) if channel is None: channel = ctx.message.channel callback = partial(self.fetch_callback, cookie) task = self.fetch_task([channel], subfolder, attachments=attachments, status_cb=callback) self.fetch_handle = self.bot.loop.create_task(task)
def set_channel(self, ctx, on_off: bool, channel: discord.Channel = None): """Sets channel logging on or off. Optional channel parameter. To enable or disable all channels at once, use `logset server`.""" if channel is None: channel = ctx.message.channel server = channel.server if server.id not in self.settings: self.settings[server.id] = {} self.settings[server.id][channel.id] = on_off if on_off: await self.bot.say('Logging enabled for %s' % channel.mention) else: await self.bot.say('Logging disabled for %s' % channel.mention) self.save_json()
def on_channel_update(self, before, after): if type(before) is discord.PrivateChannel: return entries = [] if before.name != after.name: entries.append('Channel rename: %s renamed to %s' % (before, after)) if before.topic != after.topic: entries.append('Channel topic: %s topic was set to "%s"' % (before, after.topic)) if before.position != after.position: entries.append('Channel position: {0.name} moved from {0.position} ' 'to {1.position}'.format(before, after)) # TODO: channel permissions overrides for e in entries: await self.log(before.server, e)
def on_channel_update(before: discord.Channel, after: discord.Channel): """ Update the changelog when a channel changes name. """ if after.is_private: return changelog_channel = get_changelog_channel(after.server) if not changelog_channel: return # We only want to update when a name change is performed if before.name == after.name: return # Differ between voice channels and text channels if after.type == discord.ChannelType.text: await client.send_message( changelog_channel, "Channel **#{0.name}** changed name to {1.mention}, **{1.name}**.".format(before, after)) else: await client.send_message( changelog_channel, "Voice channel **{0.name}** changed name to **{1.name}**.".format(before, after))
def can_use_command(cmd: Command, author, channel: discord.Channel=None): """ Return True if the member who sent the message can use this command. """ if cmd.owner and not is_owner(author): return False if channel is not None and not has_permissions(cmd, author, channel): return False if not has_roles(cmd, author): return False # Handle server specific commands for both server and PM commands if type(author) is discord.User and cmd.servers: return False if type(author) is discord.Member and not is_valid_server(cmd, author.server): return False return True
def run_coro(self, event): channel = self.bot.get_channel(event.channel) try: server = channel.server prefix = self.bot.settings.get_prefixes(server)[0] except AttributeError: log.debug("Channel no longer found, not running scheduled event.") return data = {} data['timestamp'] = time.strftime("%Y-%m-%dT%H:%M:%S%z", time.gmtime()) data['id'] = randint(10**(17), (10**18) - 1) data['content'] = prefix + event.command data['channel'] = self.bot.get_channel(event.channel) data['author'] = {'id': event.author} data['nonce'] = randint(-2**32, (2**32) - 1) data['channel_id'] = event.channel data['reactions'] = [] fake_message = discord.Message(**data) # coro = self.bot.process_commands(fake_message) log.info("Running '{}' in {}".format(event.name, event.server)) # self.bot.loop.create_task(coro) self.bot.dispatch('message', fake_message)
def channel_allow(self, ctx, command, channel: discord.Channel=None): """Explicitly allows [command/cog] to be used in [channel]. Not really useful because role perm overrides channel perm""" server = ctx.message.server try: command_obj = self._get_command(command) except BadCommand as e: try: self.bot.cogs[command] command_obj = command except KeyError: raise e if channel is None: channel = ctx.message.channel await self._set_permission(command_obj, server, channel=channel) await self.bot.say("Channel {} allowed use of {}.".format( channel.mention, command))
def channel_deny(self, ctx, command, channel: discord.Channel=None): """Explicitly denies [command/cog] usage in [channel] Overridden by role based permissions""" server = ctx.message.server try: command_obj = self._get_command(command) except BadCommand as e: try: self.bot.cogs[command] command_obj = command except KeyError: raise e if channel is None: channel = ctx.message.channel await self._set_permission(command_obj, server, channel=channel, allow=False) await self.bot.say("Channel {} denied use of {}.".format( channel.mention, command))
def modlog(self, ctx, channel : discord.Channel=None): """Sets a channel as mod log Leaving the channel parameter empty will deactivate it""" server = ctx.message.server if channel: self.settings[server.id]["mod-log"] = channel.id await self.bot.say("Mod events will be sent to {}" "".format(channel.mention)) else: if self.settings[server.id]["mod-log"] is None: await send_cmd_help(ctx) return self.settings[server.id]["mod-log"] = None await self.bot.say("Mod log deactivated.") dataIO.save_json("data/mod/settings.json", self.settings)
def ignore_channel(self, ctx, channel: discord.Channel=None): """Ignores channel Defaults to current one""" current_ch = ctx.message.channel if not channel: if current_ch.id not in self.ignore_list["CHANNELS"]: self.ignore_list["CHANNELS"].append(current_ch.id) dataIO.save_json("data/mod/ignorelist.json", self.ignore_list) await self.bot.say("Channel added to ignore list.") else: await self.bot.say("Channel already in ignore list.") else: if channel.id not in self.ignore_list["CHANNELS"]: self.ignore_list["CHANNELS"].append(channel.id) dataIO.save_json("data/mod/ignorelist.json", self.ignore_list) await self.bot.say("Channel added to ignore list.") else: await self.bot.say("Channel already in ignore list.")
def unignore_channel(self, ctx, channel: discord.Channel=None): """Removes channel from ignore list Defaults to current one""" current_ch = ctx.message.channel if not channel: if current_ch.id in self.ignore_list["CHANNELS"]: self.ignore_list["CHANNELS"].remove(current_ch.id) dataIO.save_json("data/mod/ignorelist.json", self.ignore_list) await self.bot.say("This channel has been removed from the ignore list.") else: await self.bot.say("This channel is not in the ignore list.") else: if channel.id in self.ignore_list["CHANNELS"]: self.ignore_list["CHANNELS"].remove(channel.id) dataIO.save_json("data/mod/ignorelist.json", self.ignore_list) await self.bot.say("Channel removed from ignore list.") else: await self.bot.say("That channel is not in the ignore list.")
def status(self, ctx): """Shows the servers settings for welcomer.""" db = fileIO(self.load, "load") if ctx.message.server.id not in db: await self.bot.say(":x: **Welcomer has not been configured for this server, use `welcomer channel` first**") return server = ctx.message.server color = ''.join([choice('0123456789ABCDEF') for x in range(6)]) color = int(color, 16) e = discord.Embed(colour=discord.Colour(value=color), description="\n\a") role = discord.utils.get(ctx.message.server.roles, id=db[server.id]["botrole"]) e.set_author(name="Settings for " + server.name, icon_url=server.icon_url) e.add_field(name="Welcomer Channel:", value="#" + self.bot.get_channel(db[server.id]["Channel"]).name if self.bot.get_channel(db[server.id]["Channel"]) else None, inline=True) e.add_field(name="Join Toggle:", value=db[server.id]["join"], inline=True) e.add_field(name="Leave Toggle:", value=db[server.id]["leave"], inline=True) e.add_field(name="Bot Role:", value=role.name if role else None) e.add_field(name="Bot Role Toggle:", value=db[server.id]["botroletoggle"]) e.add_field(name="Embed", value=db[server.id]["Embed"], inline=True) e.add_field(name="Leave Message:", value=db[server.id]["leavemessage"], inline=False) e.add_field(name="Join Message:", value=db[server.id]["joinmessage"], inline=False) try: await self.bot.say(embed=e) except Exception as e: await self.bot.say(e)
def channel(self, ctx, *, channel : discord.Channel=None): """Sets the welcomer channel settings.""" if channel is None: channel = ctx.message.channel server = ctx.message.server db = fileIO(self.load, "load") if server.id in db: db[server.id]['Channel'] = channel.id fileIO(self.load, "save", db) await self.bot.say("Channel changed.") return if not ctx.message.server.me.permissions_in(channel).manage_channels: await self.bot.say("I dont have the `manage_channels` permission.") return if ctx.message.server.me.permissions_in(channel).send_messages: if not server.id in db: db[server.id] = settings db[server.id]['Channel'] = channel.id invs = await self.bot.invites_from(server) for i in invs: db[server.id]["Invites"][i.url] = i.uses fileIO(self.load, "save", db) await self.bot.say("Channel set.")
def on_member_remove(self, member): server = member.server db = fileIO(self.load, "load") if not server.id in db: return if db[server.id]['leave'] is False: return message = db[server.id]['leavemessage'] channel = db[server.id]["Channel"] if db[server.id]["Embed"]: color = ''.join([choice('0123456789ABCDEF') for x in range(6)]) color = int(color, 16) data = discord.Embed(title="Member Left!".format(member.id), description=message.format(member, server), colour=discord.Colour(value=color)) data.set_thumbnail(url=member.avatar_url) await self.bot.send_message(server.get_channel(channel), embed=data) else: await self.bot.send_message(server.get_channel(channel), message.format(member, server))
def channel(self, ctx, channel: discord.Channel, status: str): """Sets whether or not the channel will be locked down if a lockdown is turned on Options for status are on or off""" server = ctx.message.server new_status = None if status.lower() != "on" and status.lower() != "off": await self.bot.say("Invalid status specified!") return else: if status.lower() == "on": new_status = True else: new_status = False if server.id not in self.settings: self.settings[server.id] = {} if "channels" not in self.settings[server.id]: self.settings[server.id]["channels"] = {} if channel.id not in self.settings[server.id]["channels"]: self.settings[server.id]["channels"][channel.id] = None self.settings[server.id]["channels"][channel.id] = new_status dataIO.save_json("data/lockdown/settings.json", self.settings) await self.bot.say("New status for {} set!".format(channel.mention))
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 trackmentions(self, ctx, status: str, channel: discord.Channel=None): """Toggles mention tracking for the specified channel. Defaults to the current channel.""" if not channel: channel = ctx.message.channel author = ctx.message.author if author.id not in self.settings: self.settings[author.id] = {} if channel.id not in self.settings[author.id]: self.settings[author.id][channel.id] = None if status.lower() == "on" or status.lower() == "off": self.settings[author.id][channel.id] = True if status.lower() == "on" else False dataIO.save_json("data/mentiontracker/settings.json", self.settings) await self.bot.say("Mention tracker toggled!") else: await self.bot.say("Invalid status specified!")
def listwords(self, ctx: commands.Context, channel: discord.Channel=None): """List all words for the specified channel Optional parameters: - channel: the channel to show words for (defaults to the current channel)""" author = ctx.message.author if not channel: channel = ctx.message.channel if author.id not in self.settings or\ "words" not in self.settings[author.id] or\ channel.id not in self.settings[author.id]["words"] or\ not self.settings[author.id]["words"][channel.id]: await self.bot.say("You haven't set any words to be tracked!") else: head = "Tracked words for {}#{} in #{}".format(author.name, author.discriminator, channel.name) msg = "" for word in self.settings[author.id]["words"][channel.id]: msg += "{}\n".format(word) await self.bot.say(box(msg, lang=head))
def notifications(self, ctx, channel: discord.Channel): """This command is used to set a channel as the server's 'notifications' channel Any notifications (like someone going live on Twitch, or Picarto) will go to that channel EXAMPLE: !alerts #alerts RESULT: No more alerts spammed in #general!""" if str(channel.type) != "text": await self.bot.say("The notifications channel must be a text channel!") return key = ctx.message.server.id entry = {'server_id': key, 'notification_channel': channel.id} if not await utils.update_content('server_settings', entry, key): await utils.add_content('server_settings', entry) await self.bot.say("I have just changed this server's 'notifications' channel" "\nAll notifications will now go to `{}`".format(channel))
def join_channel(self, channel: Channel): if channel.server != self.server: raise ValueError("Attempted to join a channel on a different server.") if self.voice is not None: if self.voice.channel.id == channel.id: logger.info("Tried to join a channel that we are already on. Ignoring.") return logger.info("Moving channel.") await self.voice.move_to(channel) self.find_voice_client() return try: self.voice = await client.join_voice_channel(channel) except: self.find_voice_client() logger.info("Joined channel %s, voice client: %s", channel.name, self.voice)
def channel(self, ctx, channel : discord.Channel=None): """Sets the channel to send the welcome message If channel isn't specified, the server's default channel will be used""" server = ctx.message.server if channel is None: channel = ctx.message.server.default_channel if not server.get_member(self.bot.user.id ).permissions_in(channel).send_messages: await self.bot.say("I do not have permissions to send " "messages to {0.mention}".format(channel)) return self.settings[server.id]["CHANNEL"] = channel.id dataIO.save_json(settings_path, self.settings) channel = self.get_welcome_channel(server) await self.bot.send_message(channel, "I will now send welcome " "messages to {0.mention}".format(channel)) await self.send_testing_msg(ctx)
def blacklist_add(self, ctx, value:str, *, obj:MultiMention): if isinstance(obj, discord.Server): kwargs = dict(server_id=int(obj.id)) elif isinstance(obj, discord.Channel): kwargs = dict(channel_id=int(obj.id)) elif isinstance(obj, discord.Role): kwargs = dict(role_id=int(obj.id)) elif isinstance(obj, discord.Member): kwargs = dict(user_id=int(obj.id)) with self.bot.db_scope() as session: blacklist_obj = session.query(sql.Blacklist).filter_by(**kwargs, data=value).first() if blacklist_obj is not None: await self.bot.say(f"{obj.__class__.__name__} **{str(obj)}** has already been blacklisted for `{value}`") return else: blacklist_obj = sql.Blacklist(**kwargs, data=value) session.add(blacklist_obj) await self.bot.say(f"Blacklisted {obj.__class__.__name__} **{str(obj)}** for `{value}`")
def blacklist_remove(self, ctx, value:str, *, obj:MultiMention): if isinstance(obj, discord.Server): kwargs = dict(server_id=int(obj.id)) elif isinstance(obj, discord.Channel): kwargs = dict(channel_id=int(obj.id)) elif isinstance(obj, discord.Role): kwargs = dict(role_id=int(obj.id)) elif isinstance(obj, discord.Member): kwargs = dict(user_id=int(obj.id)) with self.bot.db_scope() as session: blacklist_obj = session.query(sql.Blacklist).filter_by(**kwargs, data=value).first() if blacklist_obj is None: await self.bot.say(f"{obj.__class__.__name__} **{str(obj)}** is not blacklisted for `{value}`") return else: session.delete(blacklist_obj) await self.bot.say(f"Removed {obj.__class__.__name__} **{str(obj)}** from blacklist for `{value}`")
def blacklist_search(self, ctx, *, obj:MultiMention): if isinstance(obj, discord.Server): kwargs = dict(server_id=int(obj.id)) elif isinstance(obj, discord.Channel): kwargs = dict(channel_id=int(obj.id)) elif isinstance(obj, discord.Role): kwargs = dict(role_id=int(obj.id)) elif isinstance(obj, discord.Member): kwargs = dict(user_id=int(obj.id)) with self.bot.db_scope() as session: blacklist_objs = session.query(sql.Blacklist).filter_by(**kwargs).all() if len(blacklist_objs) > 0: result_text = f"```md\n# {obj.__class__.__name__} {str(obj)} is blacklisted for\n" + "\n".join(f"- {b_obj.data}" for b_obj in blacklist_objs) + "\n```" else: result_text = f"```md\n# {obj.__class__.__name__} {str(obj)} is not blacklisted\n```" await self.bot.say(result_text)
def channel_cmd(self, ctx, *, channel : discord.Channel = None): """Ignores a specific channel from being processed. If no channel is specified, the current channel is ignored. If a channel is ignored then the bot does not process commands in that channel until it is unignored. """ if channel is None: channel = ctx.message.channel ignored = self.config.get('ignored', []) if channel.id in ignored: await self.bot.say('That channel is already ignored.') return ignored.append(channel.id) await self.config.put('ignored', ignored) await self.bot.say('**Done!** The channel is ignored.')
def unignore(self, ctx, *channels: discord.Channel): """Unignores channels from being processed. If no channels are specified, it unignores the current channel. To use this command you must have the Manage Channels permission or have the Bot Admin role. """ if len(channels) == 0: channels = (ctx.message.channel,) # a set is the proper data type for the ignore list # however, JSON only supports arrays and objects not sets. ignored = self.config.get('ignored', []) for channel in channels: try: ignored.remove(channel.id) except ValueError: pass await self.config.put('ignored', ignored) await self.bot.say('**Done!** The channels are unignored.')
def pinmsgdate(self, ctx, date: date, *, channel: discord.Channel = 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. """ if channel is None: channel = ctx.message.channel async for m in self.bot.logs_from(channel, after=date, limit=1): try: await self.bot.pin_message(m) except: await self.bot.say('**Error!** Could not pin message.') else: await self.bot.say('**Done!** Successfully pinned message.')
def channel(self, ctx, channel: discord.Channel=None): """Sets the channel to send the update announcement If channel isn't specified, the server's default channel will be used""" server = ctx.message.server if channel is None: channel = ctx.message.server.default_channel if not server.get_member(self.bot.user.id ).permissions_in(channel).send_messages: await self.bot.say("I do not have permissions to send " "messages to {0.mention}".format(channel)) return self.settings[server.id]["CHANNEL"] = channel.id dataIO.save_json('data/guildwars2/settings.json', self.settings) channel = self.get_announcement_channel(server) await self.bot.send_message(channel, "I will now send build announcement " "messages to {0.mention}".format(channel))
def _channel(self, ctx: commands.Context, channel: discord.Channel=None): """Change le channel où doit être envoyé les messages d'activation de trigger. Par défaut le présent.""" await self.bot.type() server = ctx.message.server if not channel: channel = server.default_channel if not self.speak_permissions(server, channel): await self.bot.say( "Je n'ai pas les permissions d'envoyer de message sur {0.mention}.".format(channel)) return self.settings[server.id]["channel"] = channel.id dataIO.save_json(self.settings_path, self.settings) channel = self.get_welcome_channel(server) await self.bot.send_message(channel,"{0.mention}, " + "Je vais maintenant envoyer les messages d'annonce" + "sur {1.mention}.".format(ctx.message.author, channel))
def on_message_delete(self, message): server = message.server member = message.author channel = message.channel timestamp = datetime.utcnow() if await self._ignore(server, member=member, channel=channel): if await self._validate_event(server) and member.id != self.bot.user.id: embed = discord.Embed(color=self.red) avatar = member.avatar_url if member.avatar else member.default_avatar_url embed.set_author(name='Message removed', icon_url=avatar) embed.add_field(name='**Member**', value='{0.display_name}#{0.discriminator} ({0.id})'.format(member)) embed.add_field(name='**Channel**', value=message.channel.name) embed.add_field(name='**Message timestamp**', value=message.timestamp.strftime('%Y-%m-%d %H:%M:%S')) embed.add_field(name='**Removal timestamp**', value=timestamp.strftime('%Y-%m-%d %H:%M:%S')) if message.content: embed.add_field(name='**Message**', value=message.content, inline=False) if message.attachments: for attachment in message.attachments: embed.add_field(name='**Attachment**', value='[{filename}]({url})'.format(**attachment), inline=True) await self._send_message_to_channel(server, embed=embed)
def on_message_edit(self, before, after): server = after.server member = after.author channel = after.channel timestamp = datetime.utcnow() if not channel.is_private: if await self._ignore(server, member=member, channel=channel): if await self._validate_event(server) and member.id != self.bot.user.id and before.clean_content != after.clean_content: embed = discord.Embed(color=self.blue) avatar = member.avatar_url if member.avatar else member.default_avatar_url embed.set_author(name='Message changed'.format(member), icon_url=avatar) embed.add_field(name='**Member**', value='{0.display_name}#{0.discriminator}\n({0.id})'.format(member)) embed.add_field(name='**Channel**', value=before.channel.name) embed.add_field(name='**Message timestamp**', value=before.timestamp.strftime('%Y-%m-%d %H:%M:%S')) embed.add_field(name='**Edit timestamp**', value=timestamp.strftime('%Y-%m-%d %H:%M:%S')) embed.add_field(name='**Before**', value=before.content, inline=False) embed.add_field(name='**After**', value=after.content, inline=False) await self._send_message_to_channel(server, embed=embed)
def _ignore(self, context, channel: discord.Channel): """[channel]""" data = dataIO.load_json(self.ignore_file) current_server = context.message.server.id current_channel = channel.id if current_server not in data: data[current_server] = [] if current_channel not in data[current_server]: data[current_server].append(current_channel) message = 'Ignoring {}'.format(channel.mention) else: data[current_server].remove(current_channel) message = 'Unignoring {}'.format(channel.mention) dataIO.save_json(self.ignore_file, data) await self.bot.say('*{}*'.format(message))