def streamlock_lockmsg(self,
ctx: commands.Context,
*,
message: str=None):
"""Set the message for when the channel is locked.
Leave <message> blank to see the current message.
To include the name of the stream in the message, simply
use the placeholder {stream} in the message."""
channel = ctx.message.channel
settings = self._load(channel=channel)
if message is None:
await self.bot.send_cmd_help(ctx)
await self.bot.say("Current message:\n{}"
"".format(box(settings["LOCK_MSG"])))
return
settings["LOCK_MSG"] = message
await self.bot.say("Done. Sending test message here...")
await self.send_lock_msg("ExampleStream", channel)
self._save(settings, channel=channel)
python类Context()的实例源码
def streamlock_unlockmsg(self,
ctx: commands.Context,
*,
message: str=None):
"""Set the message for when the channel is unlocked.
Leave <message> blank to see the current message.
To include the name of the stream in the message, simply
use the placeholder {stream} in the message."""
channel = ctx.message.channel
settings = self._load(channel=channel)
if message is None:
await self.bot.send_cmd_help(ctx)
await self.bot.say("Current message:\n{}"
"".format(box(settings["UNLOCK_MSG"])))
return
settings["UNLOCK_MSG"] = message
await self.bot.say("Done. Sending test message here...")
await self.send_lock_msg("ExampleStream", channel, unlock=True)
self._save(settings, channel=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 _delay(self, ctx: commands.Context, seconds: int):
"""Sets the delay between game changes.
Must be at least 15 seconds.
"""
if seconds < 15:
await self.bot.reply(
cf.error("Delay must be at least 15 seconds."))
return
self.settings["delay"] = seconds
dataIO.save_json(self.settings_path, self.settings)
await self.bot.reply(
cf.info("Delay set to {} seconds.".format(seconds)))
def _del(self, ctx: commands.Context, *, game: str):
"""Removes a game from the list."""
try:
self.settings["games"].remove(game)
except ValueError:
await self.bot.reply(
cf.warning("{} is not in the game list.".format(game)))
return
self.settings["del"].append(game)
dataIO.save_json(self.settings_path, self.settings)
await self.bot.reply(
cf.info("{} removed from the game list.".format(game)))
def _set(self, ctx: commands.Context, *games: str):
"""Replaces the game list with the given list."""
games_str = ", ".join(sorted(list(games)))
await self.bot.reply(cf.question(
"You are about to replace the current game list with this:{}"
"Are you sure you want to proceed? (yes/no)".format(
cf.box(games_str))))
answer = await self.bot.wait_for_message(timeout=15,
author=ctx.message.author)
if answer is None or answer.content.lower().strip() != "yes":
await self.bot.reply("Game list not replaced.")
return
self.settings["del"] += self.settings["games"]
self.settings["games"] = list(games)
dataIO.save_json(self.settings_path, self.settings)
await self.bot.reply(cf.info("Game list replaced."))
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 _changeanswer(self, ctx: commands.Context,
survey_id: str):
"""Changes the calling user's response for the given survey."""
user = ctx.message.author
server_id = self._get_server_id_from_survey_id(survey_id)
if survey_id in self.surveys["closed"]:
await self.bot.send_message(user,
cf.error("That survey is closed."))
return
if not server_id:
await self.bot.send_message(user, cf.error(
"Survey with ID {} not found.".format(survey_id)))
return
new_task = self.bot.loop.create_task(
self._send_message_and_wait_for_message(server_id,
survey_id, user,
change=True))
self.tasks[survey_id].append(new_task)
def _setsteamid(self, ctx: commands.Context, steamID: str):
"""Associates your Discord account to the Steam profile
with the given ID.
You MUST provide your text ID, which has the form 'STEAM_X:X:XXXXXX'.
You can use http://steamidfinder.com/ to get it.
"""
if re.compile(r"STEAM_\d:\d:\d+").match(steamID) is None:
await self.bot.reply(cf.error(
"Provided Steam ID does not seem to be in the correct format. "
"You need to provide the ID of the form 'STEAM_X:X:XXXXXX'. "
"You can use http://steamidfinder.com/ to get it."))
return
server = ctx.message.server
self.settings[server.id]["steam_ids"][steamID.split(":")[-1]] = ctx.message.author.id
dataIO.save_json(self.settings_path, self.settings)
await self.bot.reply(cf.info("Steam ID set."))
def _addquote(self, ctx: commands.Context, *,
new_quote: str):
"""Adds a new quote."""
await self.bot.type()
server = ctx.message.server
if server.id not in self.settings:
self.settings[server.id] = deepcopy(default_settings)
dataIO.save_json(self.settings_path, self.settings)
idx = self.settings[server.id]["next_index"]
self.settings[server.id]["quotes"][str(idx)] = new_quote
self.settings[server.id]["next_index"] += 1
dataIO.save_json(self.settings_path, self.settings)
await self.bot.reply(
cf.info("Quote added as number {}.".format(idx)))
def _profile(self, ctx: commands.Context,
player: str, platform: str="uplay"):
"""Get the profile for the given player.
'platform' must be one of 'uplay', 'xbox', or 'playstation', and
defaults to 'uplay'.
"""
p = await self.get_player(player, platform)
if p is None:
return
await p.check_general()
await p.check_level()
e = discord.Embed(description="Player Summary")
e.set_author(name=p.name, url=p.url)
e.set_thumbnail(url=p.icon_url)
e.add_field(name="Level", value=p.level)
e.add_field(name="XP", value=p.xp)
e.add_field(name="Platform", value=p.platform)
await self.bot.say(embed=e)
def _toggle(self, ctx: commands.Context):
"""Toggles StreamRole on/off."""
await self.bot.type()
server = ctx.message.server
if (not self.settings[server.id]["enabled"] and
self.settings[server.id]["role"] is None):
await self.bot.reply(cf.warning(
"You need to set the role before turning on StreamRole."
" Use `{}streamroleset role`".format(ctx.prefix)))
return
self.settings[server.id][
"enabled"] = not self.settings[server.id]["enabled"]
if self.settings[server.id]["enabled"]:
await self.bot.reply(
cf.info("StreamRole is now enabled."))
else:
await self.bot.reply(
cf.info("StreamRole is now disabled."))
dataIO.save_json(self.settings_path, self.settings)
def _membershipset(self, ctx: commands.Context):
"""Sets membership settings."""
server = ctx.message.server
if server.id not in self.settings:
self.settings[server.id] = deepcopy(default_settings)
self.settings[server.id]["channel"] = server.default_channel.id
dataIO.save_json(self.settings_path, self.settings)
if ctx.invoked_subcommand is None:
await self.bot.send_cmd_help(ctx)
msg = "```"
msg += "ON: {}\n".format(self.settings[server.id]["on"])
msg += "CHANNEL: #{}\n".format(self.get_welcome_channel(server))
msg += "JOIN: {}\n".format(self.settings[server.id]["join_message"])
msg += "LEAVE: {}\n".format(self.settings[server.id]["leave_message"])
msg += "BAN: {}\n".format(self.settings[server.id]["ban_message"])
msg += "UNBAN: {}\n".format(self.settings[server.id]["unban_message"])
msg += "```"
await self.bot.say(msg)
def _jumptop(self, ctx: commands.Context):
"""Gets the top stats for the given jump type.
Optionally provide a limit (default is 10).
"""
await self.bot.type()
server = ctx.message.server
if server.id not in self.settings:
self.settings[server.id] = deepcopy(default_settings)
dataIO.save_json(self.settings_path, self.settings)
if not self._check_settings(server.id):
await self.bot.reply(
cf.error("You need to set up this cog before you can use it."
" Use `{}kzset`.".format(ctx.prefix)))
return
await self._update_database(server.id)
if ctx.invoked_subcommand is None:
await ctx.invoke(self._all)
def say_and_pm(ctx, content):
"""Send message to current channel as well as the command message's author.
`ctx` can be either `discord.Message` or `commands.Context`
"""
channel = ctx.channel
author = ctx.author
to_say = content.format(channel='')
to_pm = content.format(channel=f'in {channel.mention}')
return (await ctx.send(to_say),
await author.send(to_pm))
def get_server(context: Context) -> discord.Server:
""" Gets the server to which a command was sent,
based on the command's context.
:param context: The context in which the command was sent.
:type context: discord.ext.commands.Context
:return: The server
"""
return context.message.server
def get_server_id(context: Context) -> str:
""" Gets the ID of the server to which a command was sent,
based on the command's context.
:param context: The context in which the command was sent.
:type context: discord.ext.commands.Context
:return: The server's ID
"""
server = get_server(context)
return None if server is None else server.id
def get_channel(context: Context) -> discord.Channel:
""" Gets a channel to which a command was sent, based on the command's
context.
:param context: The context in which the command was sent.
:type context: discord.ext.commands.Context
:return: The channel
"""
return context.message.channel
def get_channel_id(context: Context) -> str:
""" Gets the ID of the channel to which a command was sent,
based on the command's context.
:param context: The context in which the command was sent.
:type context: discord.ext.commands.Context
:return: The channel's ID
"""
return get_channel(context).id
def get_channel_name(context: Context) -> str:
""" Gets the name of the channel to which a command was sent,
based on the command's context.
:param context: The context in which the command was sent.
:type context: discord.ext.commands.Context
:return: The channel's name
"""
return get_channel(context).name