本文整理汇总了Python中discord.Object方法的典型用法代码示例。如果您正苦于以下问题:Python discord.Object方法的具体用法?Python discord.Object怎么用?Python discord.Object使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类discord
的用法示例。
在下文中一共展示了discord.Object方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: subscribe_command
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def subscribe_command(self, ctx: Context, *_) -> None: # We don't actually care about the args
"""Subscribe to announcement notifications by assigning yourself the role."""
has_role = False
for role in ctx.author.roles:
if role.id == constants.Roles.announcements:
has_role = True
break
if has_role:
await ctx.send(f"{ctx.author.mention} You're already subscribed!")
return
log.debug(f"{ctx.author} called !subscribe. Assigning the 'Announcements' role.")
await ctx.author.add_roles(Object(constants.Roles.announcements), reason="Subscribed to announcements")
log.trace(f"Deleting the message posted by {ctx.author}.")
await ctx.send(
f"{ctx.author.mention} Subscribed to <#{constants.Channels.announcements}> notifications.",
)
示例2: unsubscribe_command
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def unsubscribe_command(self, ctx: Context, *_) -> None: # We don't actually care about the args
"""Unsubscribe from announcement notifications by removing the role from yourself."""
has_role = False
for role in ctx.author.roles:
if role.id == constants.Roles.announcements:
has_role = True
break
if not has_role:
await ctx.send(f"{ctx.author.mention} You're already unsubscribed!")
return
log.debug(f"{ctx.author} called !unsubscribe. Removing the 'Announcements' role.")
await ctx.author.remove_roles(Object(constants.Roles.announcements), reason="Unsubscribed from announcements")
log.trace(f"Deleting the message posted by {ctx.author}.")
await ctx.send(
f"{ctx.author.mention} Unsubscribed from <#{constants.Channels.announcements}> notifications."
)
# This cannot be static (must have a __func__ attribute).
示例3: proxy_user
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def proxy_user(user_id: str) -> discord.Object:
"""
Create a proxy user object from the given id.
Used when a Member or User object cannot be resolved.
"""
log.trace(f"Attempting to create a proxy user for the user id {user_id}.")
try:
user_id = int(user_id)
except ValueError:
log.debug(f"Failed to create proxy user {user_id}: could not convert to int.")
raise BadArgument(f"User ID `{user_id}` is invalid - could not convert to an integer.")
user = discord.Object(user_id)
user.mention = user.id
user.display_name = f"<@{user.id}>"
user.avatar_url_as = lambda static_format: None
user.bot = False
return user
示例4: convert
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def convert(self, ctx: Context, arg: str) -> t.Union[discord.User, discord.Object]:
"""Convert the `arg` to a `discord.User` or `discord.Object`."""
try:
return await super().convert(ctx, arg)
except BadArgument:
pass
try:
user_id = int(arg)
log.trace(f"Fetching user {user_id}...")
return await ctx.bot.fetch_user(user_id)
except ValueError:
log.debug(f"Failed to fetch user {arg}: could not convert to int.")
raise BadArgument(f"The provided argument can't be turned into integer: `{arg}`")
except discord.HTTPException as e:
# If the Discord error isn't `Unknown user`, return a proxy instead
if e.code != 10013:
log.info(f"Failed to fetch user, returning a proxy instead: status {e.status}")
return proxy_user(arg)
log.debug(f"Failed to fetch user {arg}: user does not exist.")
raise BadArgument(f"User `{arg}` does not exist")
示例5: _load_text_channels
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def _load_text_channels(self):
log.debug(f"Loading text channels on {self.guild.id}")
for tchannel in self.data["text_channels"]:
try:
created = await self.guild.create_text_channel(
name=tchannel["name"],
overwrites=await self._overwrites_from_json(tchannel["overwrites"]),
category=discord.Object(self.id_translator.get(tchannel["category"])),
reason=self.reason
)
self.id_translator[tchannel["id"]] = created.id
await created.edit(
topic=self._translate_mentions(tchannel["topic"]),
nsfw=tchannel["nsfw"],
)
except Exception:
pass
示例6: _load_voice_channels
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def _load_voice_channels(self):
log.debug(f"Loading voice channels on {self.guild.id}")
for vchannel in self.data["voice_channels"]:
try:
created = await self.guild.create_voice_channel(
name=vchannel["name"],
overwrites=await self._overwrites_from_json(vchannel["overwrites"]),
category=discord.Object(self.id_translator.get(vchannel["category"])),
reason=self.reason
)
await created.edit(
bitrate=vchannel["bitrate"],
user_limit=vchannel["user_limit"]
)
self.id_translator[vchannel["id"]] = created.id
except Exception:
pass
示例7: ten_recent_msg
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def ten_recent_msg(self) -> List[int]:
"""Get the last 10 messages sent in the channel."""
ten_recent = []
recent_msg_id = max(
message.id for message in self.bot._connection._messages
if message.channel.id == Channels.seasonalbot_commands
)
channel = await self.hacktober_channel()
ten_recent.append(recent_msg_id)
for i in range(9):
o = discord.Object(id=recent_msg_id + i)
msg = await next(channel.history(limit=1, before=o))
ten_recent.append(msg.id)
return ten_recent
示例8: on_channel_delete
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def on_channel_delete(self, channel):
try:
sql = "SELECT server,channel FROM `logs` WHERE server={0}"
sql = sql.format(channel.server.id)
result = self.cursor.execute(sql).fetchall()
if len(result) == 0:
return
chan = str(result[0]['channel'])
if channel.id == chan:
remove_sql = "DELETE FROM `logs` WHERE server={0}"
remove_sql = remove_sql.format(channel.server.id)
self.cursor.execute(remove_sql)
self.cursor.commit()
return
server = str(result[0]['server'])
if channel.server.id != server:
return
a = channel.server
msg = "{0} Channel: {1} <{2}>\n".format("Voice" if channel.type == discord.ChannelType.voice else "Text", channel.name, channel.id).replace("'", "")
target = discord.Object(id=chan)
await self.bot.send_message(target, "`[{0}]` :x: **Channel Delete Log**\n".format(time.strftime("%I:%M:%S %p"))+cool.format(msg))
except (discord.errors.Forbidden, discord.errors.NotFound, discord.errors.InvalidArgument):
self.remove_server(channel.server)
示例9: on_member_join
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def on_member_join(self, member):
try:
if member == self.bot.user:
return
check = await self.is_ignored(None, user=True, global_only=True, u=member, server=member.server)
if check:
return
sql = "SELECT server,channel FROM `logs` WHERE server={0}"
sql = sql.format(member.server.id)
result = self.cursor.execute(sql).fetchall()
if len(result) == 0:
return
channel = str(result[0]['channel'])
server = str(result[0]['server'])
if member.server.id != server:
return
a = member.server
msg = "Member Joined: {0.name} <{0.id}>\n".format(member)
msg += "Server: {0.name} <{0.id}>\n".format(a).replace("'", "")
target = discord.Object(id=channel)
await self.bot.send_message(target, "`[{0}]` :inbox_tray: **Member Join Log**\n".format(time.strftime("%I:%M:%S %p"))+cool.format(msg))
except (discord.errors.Forbidden, discord.errors.NotFound, discord.errors.InvalidArgument):
self.remove_server(member.server)
示例10: on_member_remove
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def on_member_remove(self, member):
try:
if member == self.bot.user:
return
check = await self.is_ignored(None, user=True, global_only=True, u=member, server=member.server)
if check:
return
sql = "SELECT server,channel FROM `logs` WHERE server={0}"
sql = sql.format(member.server.id)
result = self.cursor.execute(sql).fetchall()
if len(result) == 0:
return
if member.server.id in self.banned_users:
if member.id in self.banned_users[member.server.id]:
return
channel = str(result[0]['channel'])
server = str(result[0]['server'])
if member.server.id != server:
return
a = member.server
msg = "User: {0.name} <{0.id}>\n".format(member)
target = discord.Object(id=channel)
await self.bot.send_message(target, "`[{0}]` :outbox_tray: **Member Leave/Kick Log**\n".format(time.strftime("%I:%M:%S %p"))+cool.format(msg))
except (discord.errors.Forbidden, discord.errors.NotFound, discord.errors.InvalidArgument):
self.remove_server(member.server)
示例11: on_member_ban
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def on_member_ban(self, member):
try:
if member == self.bot.user:
return
check = await self.is_ignored(None, user=True, global_only=True, u=member, server=member.server)
if check:
return
sql = "SELECT server,channel FROM `logs` WHERE server={0}"
sql = sql.format(member.server.id)
result = self.cursor.execute(sql).fetchall()
if len(result) == 0:
return
if member.server.id in self.banned_users:
self.banned_users[member.server.id] += member.id
else:
self.banned_users[member.server.id] = [member.id]
channel = str(result[0]['channel'])
server = str(result[0]['server'])
if member.server.id != server:
return
msg = "User: {0} <{0.id}>\n".format(member)
target = discord.Object(id=channel)
await self.bot.send_message(target, "`[{0}]` :outbox_tray: **Member Ban Log**\n".format(time.strftime("%I:%M:%S %p"))+cool.format(msg))
except (discord.errors.Forbidden, discord.errors.NotFound, discord.errors.InvalidArgument):
self.remove_server(member.server)
示例12: on_member_unban
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def on_member_unban(self, server, member):
try:
if member == self.bot.user:
return
check = await self.is_ignored(None, user=True, global_only=True, u=member, server=server)
if check:
return
sql = "SELECT server,channel FROM `logs` WHERE server={0}"
sql = sql.format(server.id)
result = self.cursor.execute(sql).fetchall()
if len(result) == 0:
return
channel = str(result[0]['channel'])
serverr = str(result[0]['server'])
if server.id != serverr:
return
msg = "Unbanned User: {0} <{0.id}>\n".format(member)
target = discord.Object(id=channel)
await self.bot.send_message(target, "`[{0}]` :white_check_mark: **Member Unban Log**\n".format(time.strftime("%I:%M:%S %p"))+cool.format(msg))
except (discord.errors.Forbidden, discord.errors.NotFound, discord.errors.InvalidArgument):
self.remove_server(server)
示例13: on_server_role_create
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def on_server_role_create(self, role):
try:
sql = "SELECT server,channel FROM `logs` WHERE server={0}"
sql = sql.format(role.server.id)
result = self.cursor.execute(sql).fetchall()
if len(result) == 0:
return
channel = str(result[0]['channel'])
server = str(result[0]['server'])
if role.server.id != server:
return
target = discord.Object(id=channel)
msg = "Server Role Created: {0}\n".format(role.name)
await self.bot.send_message(target, "`[{0}]` :bangbang: **Server Roles Create Log**\n".format(time.strftime("%I:%M:%S %p"))+cool.format(msg))
except (discord.errors.Forbidden, discord.errors.NotFound, discord.errors.InvalidArgument):
self.remove_server(role.server)
示例14: on_server_role_delete
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def on_server_role_delete(self, role):
try:
sql = "SELECT server,channel FROM `logs` WHERE server={0}"
sql = sql.format(role.server.id)
result = self.cursor.execute(sql).fetchall()
if len(result) == 0:
return
channel = str(result[0]['channel'])
server = str(result[0]['server'])
if role.server.id != server:
return
target = discord.Object(id=channel)
r_b = ', '.join(map(str, role.server.roles))
msg = "Server Role Deleted: {0}\n".format(role.name)
await self.bot.send_message(target, "`[{0}]` :bangbang: **Server Roles Delete Log**\n".format(time.strftime("%I:%M:%S %p"))+cool.format(msg))
except (discord.errors.Forbidden, discord.errors.NotFound, discord.errors.InvalidArgument):
self.remove_server(role.server)
示例15: hackban
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Object [as 别名]
def hackban(self, ctx, *users:str):
banned = []
for user in users:
u = discord.Object(id=user)
u.server = ctx.message.server
try:
await self.bot.ban(u)
banned.append(user)
except:
uu = ctx.message.server.get_member(user)
if uu is None:
await self.bot.say('`{0}` could not be hack banned.'.format(user))
else:
await self.bot.say('`{0}` is already on the server and could not be banned.'.format(uu))
continue
if banned:
await self.bot.say(':white_check_mark: Hackbanned `{0}`!'.format(", ".join(banned)))