Python discord 模块,discordException() 实例源码
我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用discord.discordException()。
def add_reactions(self):
"""Adds the reactions buttons to the current message"""
self.statuslog.info("Loading buttons")
for e in ("?", "?", "??", "??"):
try:
if self.embed is not None:
await client.add_reaction(self.embed.sent_embed, e)
except discord.discordException as e:
logger.exception(e)
self.statuslog.error("I Couldn't add the buttons. Check my permissions.")
except Exception as e:
logger.exception(e)
def vsetup(self, author):
"""Creates the voice client
Args:
author (discord.Member): The user that the voice ui will seek
"""
if self.vready:
logger.warning("Attempt to init voice 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 voice")
# Create voice client
self.vchannel = author.voice.voice_channel
if self.vchannel:
self.statuslog.info("Connecting to voice")
try:
self.vclient = await client.join_voice_channel(self.vchannel)
except discord.ClientException as e:
logger.exception(e)
self.statuslog.warning("I'm already connected to a voice channel.")
return
except discord.opus.OpusNotLoaded as e:
logger.exception(e)
logger.error("Could not load Opus. This is an error with your FFmpeg setup.")
self.statuslog.error("Could not load Opus.")
return
except discord.discordException as e:
logger.exception(e)
self.statuslog.error("I Couldn't connect to the voice channel. Check my permissions.")
return
except Exception as e:
self.statuslog.error("Internal error connecting to voice,disconnecting.")
logger.error("Error connecting to voice {}".format(e))
return
else:
self.statuslog.error("You're not connected to a voice channel.")
return
self.vready = True
def producer(self):
self.logger.info(f"{self.name}: producer coroutine started!")
# Setup
await self.client.wait_until_ready()
await self.init()
yield_delay = self.config['crawler']['delays']['yield']
long_delay = self.config['crawler']['delays']['empty-source']
done = dict.fromkeys(self.progress.keys(), False)
while True:
self._update_current()
# Round-robin between all sources:
# Tuple because the underlying dictionary may change size
for source, last_id in tuple(self.progress.items()):
if done[source] and not self.continuous:
continue
try:
events = await self.read(source, last_id)
if events is None:
# This source is exhausted
done[source] = True
await self.queue.put((source, None, self.current))
self.progress[source] = self.current
else:
# This source still has more
done[source] = False
last_id = self.get_last_id(events)
await self.queue.put((source, events, last_id))
self.progress[source] = last_id
except discord.discordException:
self.logger.error(f"{self.name}: error during event read", exc_info=1)
if all(done.values()):
self.logger.info(f"{self.name}: all sources are exhausted,sleeping for a while...")
delay = long_delay
else:
delay = yield_delay
await asyncio.sleep(delay)
def on_message(self, message):
"""
:param message:
:return:
"""
# We don't want Rero to be replying to his
# own messages right? Also a global count
# of all the sent messages is used with ?info
if message.author.id == self.user.id:
self.redis.incr("RERO_SENT_MESSAGES")
return
# To keep the bad boys from abusing our lovely bot
blacklist = self.redis.lrange("user_black_list", 0, -1)
if str(message.author.id).encode() in blacklist:
return
# We don't respond to private messages. We are too cool for
# that
if message.channel.is_private:
try:
await self.loop.create_task(
self.send_message(message.author, "Rero can only be used from a server.\n"
"If you want to kNow more about Rero,visit "
"https://github.com/voqz/rerobot/"))
return
except discord.discordException:
return
# Handles the PM mentions. We created this task first as this
# is the first priority ;)
await self.loop.create_task(self.commands.pm_mentions_handler(message))
# Message Logger hook. This is basically responsible for handling
# the logging function for Rero.
await self.loop.create_task(self.commands.log_message(message))
# Command Parser handles commands directed at Rero. This is
# responsible for parsing the commands from regular message content
# and then taking specific actions
await self.loop.create_task(self.commands.handle_message(message))
# XP from messages handles giving xp on servers with Ranking enabled
await self.loop.create_task(self.ranking.ranking_handler(message))
# Level Up announcer handles the member level up announcements on
# servers with ranking enabled and announcements enabled
await self.loop.create_task(self.ranking.level_up_announcer(message))
# Osu Link Scanner is responsible for parsing osu based links from
# messages and then taking some specific actions
await self.loop.create_task(self.osu.parse_message(message))
# Event handler codes.
# We handle each event as a task so that it does not
# end up slowing down the bot and instead do concurrent
# jobs
def on_message(self, message):
"""
:param message:
:return:
"""
# We don't want Lapzbot to be replying to his
# own messages right? Also a global count
# of all the sent messages is used with ?info
if message.author.id == self.user.id:
self.redis.incr("LAPZBOT_SENT_MESSAGES")
return
# To keep the bad boys from abusing our lovely bot
blacklist = self.redis.lrange("user_black_list", "Lapzbot can only be used from a server.\n"
"If you want to kNow more about Lapzbot,visit "
"http://lapzbot.xyz/"))
return
except discord.discordException:
return
# Handles the PM mentions. We created this task first as this
# is the first priority ;)
await self.loop.create_task(self.commands.pm_mentions_handler(message))
# Message Logger hook. This is basically responsible for handling
# the logging function for Lapzbot.
await self.loop.create_task(self.commands.log_message(message))
# Command Parser handles commands directed at Lapzbot. This is
# responsible for parsing the commands from regular message content
# and then taking specific actions
await self.loop.create_task(self.commands.handle_message(message))
# XP from messages handles giving xp on servers with Ranking enabled
await self.loop.create_task(self.ranking.ranking_handler(message))
# Level Up announcer handles the member level up announcements on
# servers with ranking enabled and announcements enabled
await self.loop.create_task(self.ranking.level_up_announcer(message))
# Osu Link Scanner is responsible for parsing osu based links from
# messages and then taking some specific actions
await self.loop.create_task(self.osu.parse_message(message))
# Event handler codes.
# We handle each event as a task so that it does not
# end up slowing down the bot and instead do concurrent
# jobs
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。