Re-commit fixed ability to send to channel ID

This commit is contained in:
archfan 2021-07-05 16:30:10 +00:00 committed by GitHub
parent eacc53532f
commit 5b23959682
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 10 deletions

10
API.md
View File

@ -4,15 +4,17 @@
It does not expose the command interface or logins to the API, and `discord.register_on_message` events will *not* recieve login information.
### `discord.send(message)`
Sends `message` to Discord.
The use of `[]` indicates an optional parameter.
### `discord.send(message, [id])`
Sends `message` to Discord, with an optional target channel ID or user ID `id`. All IDs are strings.
This function makes an HTTP request; therefore the sending of large volumes of data might be better grouped into a single request. **Do note that Discord limits messages to 2,000 characters, and the relay automatically cuts off messages.**
### `discord.register_on_message(function(name, message))`
Adds a function to `discord.registered_on_messages`, which are called every time a message is received from Discord, excluding logins. `name` is by default the Discord username of the user who sent the message (excluding the discriminator) and `message` is the message content. This function should be called on startup.
Adds a function to `discord.registered_on_messages`, which are called every time a message is received from the specified relay channel on Discord. `name` is by default the Discord username of the user who sent the message (excluding the discriminator) and `message` is the message content. This function should be called on startup.
### `discord.chat_send_all(message)`
Sends a message to all ingame (Minetest) players. This function does **not** relay to Discord. It may, however, trigger other mods which have overriding `minetest.chat_send_all`, dependent only on the capricous nature of Minetest's mod loading.
Sends a message to all ingame (Minetest) players. This function does **not** relay to Discord. It may, however, trigger other mods which have overridden `minetest.chat_send_all`, dependent only on the capricous nature of Minetest's mod loading.
### `discord.send_noirc(message)`
Sends a message to Discord, but does **not** relay said message to IRC. This function is *only* available if the IRC mod is enabled.

View File

@ -62,6 +62,15 @@ authenticated_users = {}
def check_timeout():
return time.time() - last_request <= 1
async def get_or_fetch_channel(id):
target_channel = bot.get_channel(id)
if target_channel is None:
target_channel = await bot.fetch_channel(id)
if target_channel is None:
print(f'Failed to fetch channel {id!r}.')
return target_channel
async def get_or_fetch_user(user_id):
user = bot.get_user(user_id)
if user is None:
@ -83,9 +92,9 @@ async def handle(request):
msg = r.sub('', msg)
if 'context' in data.keys():
id = int(data['context'])
user = await get_or_fetch_user(id)
if user is not None:
await user.send(msg)
target_channel = await get_or_fetch_channel(id)
if target_channel is not None:
await target_channel.send(msg)
else:
await channel.send(msg)
return web.Response(text = 'Acknowledged') # discord.send should NOT block extensively on the Lua side
@ -160,7 +169,11 @@ async def login(ctx, username, password=''):
if not logins_allowed:
return
if ctx.guild is not None:
await ctx.send(ctx.author.mention+' You\'ve quite possibly just leaked your password; it is advised that you change it at once.\n*This message will be automatically deleted*', delete_after = 10)
await ctx.send(ctx.author.mention+' You\'ve quite possibly just leaked your password by using this command outside of DMs; it is advised that you change it at once.\n*This message will be automatically deleted.*', delete_after = 10)
try:
await ctx.message.delete()
except:
print(f"Unable to delete possible password leak by user ID {ctx.author.id} due to insufficient permissions.")
return
login_queue.add({
'username' : username,