return parsed
async def maybe_say(channel, text, *, attachment = None, embed = None, probability = 0.2, wake = True):
+ destination = 'private channel [{}]'.format(', '.join(user.name, user.discriminator) for user in channel.recipients if user.id != channel.me.id) if channel.is_private else 'channel {} on {}'.format(channel.name, channel.server.name)
if random.random() <= probability:
- await client.send_typing(channel)
- message = None
- if attachment is not None:
- parsed = parse_attachment(attachment)
- if parsed is not None:
- message = await client.send_file(channel, parsed['fd'], filename = parsed['filename'], content = text)
- parsed['fd'].close()
- else:
- log.warning("Missing or invalid attachment!")
- if message is None:
- message = await client.send_message(channel, text, embed = embed)
+ try:
+ message = None
+ await client.send_typing(channel)
+ if attachment is not None:
+ parsed = parse_attachment(attachment)
+ if parsed is not None:
+ message = await client.send_file(channel, parsed['fd'], filename = parsed['filename'], content = text)
+ parsed['fd'].close()
+ else:
+ log.warning("Missing or invalid attachment!")
+ if message is None:
+ message = await client.send_message(channel, text, embed = embed)
+ except Exception:
+ log.error('sending to {}: {}'.format(destination, traceback.format_exc().splitlines()[-1]))
if wake:
await wake_up()
return message
return message
async def say_many(channel, lines, *, attachment = None, embed = None, maxlen = 2000):
- destination = 'private channel [{}]'.format(', '.join(user.name, user.discriminator) for user in channel.recipients if user.id != channel.me.id) if channel.is_private else 'channel {} on {}'.format(channel.name, channel.server.name)
while len(lines):
message_len = 0
n = 0
# We can't print too big a message.
subset = lines[:n]
message = '\n'.join(subset)
- try:
- result = await say(channel, message, attachment = attachment, embed = embed)
- except Exception:
- log.error('sending to {}: {}'.format(destination, traceback.format_exc().splitlines()[-1]))
- result = None
+ result = await say(channel, message, attachment = attachment, embed = embed)
if not result:
break
lines = lines[len(subset):]