Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reopening this PR for merging TwitchCog into develop #156

Merged
merged 56 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
3017b24
Implemented verification for POST requests to ensure the message is f…
Jun 20, 2021
878fac0
Added basic logging to TwitchListener
Jun 20, 2021
8922e88
Added TwitchApp class to handle any data the Request handler needs to…
Jun 21, 2021
1db5ff9
Added docstring to existing methods
Jun 22, 2021
8df788a
Implemented send_webhook so that notifications get sent to their resp…
Jun 23, 2021
d590143
updated requirements.txt
Jun 23, 2021
74dbd40
updated generate_schema.py
Jun 23, 2021
d68aa96
updated gitignore
Jun 23, 2021
5c00dd8
Implemented the TwitchCog with hook creation and account adding
Jun 23, 2021
a5bbd6d
Added removetwitch and listtwitch commands
Jun 23, 2021
cae9c14
Added remove twitch hook command
Jun 23, 2021
49e3fdf
Added docstrings to command methods
Jun 23, 2021
bf5f82b
Changed CALLBACK_URL to use env var
Jun 23, 2021
ab38038
addtwitch channel now accepts a string for the custom message and als…
Jun 23, 2021
407eb49
Added user facing strings
Jun 23, 2021
a95bd35
Added command to set custom message
Jun 23, 2021
7f4fe0f
Added command to get custom message
Jun 23, 2021
33404d2
Applied YAPF formatter
Jun 23, 2021
3fac2bf
Added check to not allow webhook names to be the webhook prefix
Jun 23, 2021
a8f7b92
Moved all commands to be subcommand of `twitch`
Jun 23, 2021
7272696
Changed module name to reflect new module name in bot start
Jun 24, 2021
2bba265
Ensured consistency of use of the word channel over account when refe…
Jun 24, 2021
a8fb557
Added help and usage strings to commands
Jun 24, 2021
01996b0
Fixed an bug that caused an error when given a Twitch channel that do…
Jun 24, 2021
3345c4c
Made channel names in user strings be inline code blocks for easier r…
Jun 24, 2021
9eb15c4
Fixed missing turning list into a dict for channel info
Jun 24, 2021
879479c
Added new string for channels that are not tracked but are real channels
Jun 24, 2021
6b08e35
Added check for empty environment variables
Jun 24, 2021
62cd227
Updated some logging lines
Jun 24, 2021
8da6531
Applied YAPF formatter
Jun 24, 2021
6d4a706
Fixed typos
Jun 24, 2021
9d00bf6
Added module docstring
Jun 24, 2021
e7bb9cb
Notifications now include the custom 'go live' message
Jun 24, 2021
42849c0
Added preview command to see what a notification for a channel will l…
Jun 24, 2021
7d6429a
Applied YAPF formatting
Jun 25, 2021
b22f636
Deleted TwitchIntegrationCog
Jun 26, 2021
0269886
Merge branch 'develop' into feat/TwitchCog
Jun 26, 2021
c8b13a8
Implemented new DB accessor
Jun 27, 2021
0e4ab0f
Moved load_discord_hooks to discordUtil
Jul 5, 2021
4aef65d
Made reusable functions for loading/saving bearer tokens
Jul 5, 2021
f1449de
Updated load_tracked_channels docstring to be more consistent.
Jul 5, 2021
70d6906
Fixed some booleans to be simpler
Jul 5, 2021
41f81b9
Moved more functions to discordUtil for shared access
Jul 5, 2021
abc0367
Made global twitch api strings
Jul 5, 2021
08a8725
Fixed an issue where PyCharm doesn't recognise application as the cor…
Jul 5, 2021
764b85d
Added filter to for to avoid needing to check for membership
Jul 5, 2021
e746f31
Merge branch 'develop' into feat/TwitchCog
Fluxticks Jul 6, 2021
8369748
Exposed port 443
Jul 23, 2021
47d0b7c
Merge branch 'develop' into feat/TwitchCog
Aug 6, 2021
5caf237
Fixed an issue where if the twitch temp file did not exist it would f…
Aug 6, 2021
545c7d5
Fixed an issue caused by changing of program starting dir
Aug 6, 2021
5cfdfb8
Changed createhook command to require a name and channel as parameters
Aug 6, 2021
6dd0f9d
Updated how notifications are posted for channels so that a channel n…
Aug 8, 2021
02061cb
Updated README to include the changes to Webhooks and the tracking of…
Aug 8, 2021
f7869cd
Squashed commit of the following:
Aug 19, 2021
bebf44d
Updated user_strings.toml manually as the merge wanted to overwrite t…
Aug 19, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*.env
**/__pycache__
test-file.py
/src/cogs/TestCog.py
.vscode
.idea
.idea
secrets.env
52 changes: 38 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,28 +224,52 @@ In your `.env` file the `TWITCH_SUB_SECRET` should be a string that is 10-100 ch
The `TWITCH_CALLBACK` is the URL to your HTTPS server. For testing you can use `ngrok`:
- Run `ngrok http 443` and copy the `https` URL **not** the `htttp` URL and use that as your `TWITCH_CALLBACK` variable.

#### !twitch createhook [optional: channel_mention] [optional: hook name]
* Creates a Discord Webhook bound to the channel the command was executed in, unless a channel is given, and with a default name unless a name is given.
#### !twitch createhook \<channel mention> \<hook name>
* Aliases: `newhook, makehook, addhook`
* Creates a new Discord Webhook bound to the mentioned channel.
The name will be prefixed with the Twitch Cog Webhook prefix to distinguish Twitch hooks from other Webhooks.
The Webhooks created with the Twitch Cog do not need the prefix used in the name in order to reference them.
* *Requires `administrator` permission in Discord*

#### !twitch deletehook \<hook name>
* Deletes the given Discord Webhook.
* *Requires `administrator` permission in Discord*

#### !twitch add \<twitch handle | twitch url> [optional: custom message]
* Adds a Twitch channel to be tracked in the current Discord server.
* *__If a custom message is given, it must be surrounded by double quotes__*: `!twitch add <twitch_handle> "custom_message"`
#### !twitch add \<twitch handle | twitch url> \<webhook name> [optional: custom message]
* Adds a Twitch channel to be tracked in the given Webhook.
This means when the channel goes live, its notification will be posted to the given Webhook.
A channel can be tied to more than one Webhook.
The custom message can be left empty, but when not, it will be used in the live notification.
A preview of what a notification looks like can be seen with the `!twitch preview <twitch handle> <webhook name>` command.
* *__If a custom message is given, it must be surrounded by double quotes__*: `!twitch add <twitch_handle> <webhook name> "custom_message"`
* *Requires `administrator` permission in Discord*

#### !twitch remove \<twitch handle>
* Removes a Twitch channel from being tracked in the current Discord server.
#### !twitch remove \<twitch handle> \<webhook name>
* Removes a Twitch channel from being tracked in the given Webhook.
* *Requires `administrator` permission in Discord*

#### !twitch list
* Shows a list of all the currently tracked Twitch accounts and their custom messages.
#### !twitch list [optional: webhook name]
* Shows a list of all the currently tracked Twitch accounts and their custom messages for the given Webhook.
If no Webhook name is given, it shows the information for all Twitch Webhooks.
* *Requires `administrator` permission in Discord*

#### !twitch setmessage \<twitch handle> [optional: custom message]
* Sets the custom message of a Twitch channel. Can be left empty if the custom message is to be removed.
* *__If a custom message is given, it must be surrounded by double quotes__*: `!twitch setmessage <twitch_handle> "custom_message"`
#### !twitch webhooks
* Shows a list of the current Webhooks for the Twitch Cog.
* *Requires `administrator` permission in Discord*

#### !twitch setmessage \<twitch handle> \<webhook name> [optional: custom message]
* Sets the custom message of a Twitch channel for the given Webhook.
Can be left empty if the custom message is to be removed.
* *__If a custom message is given, it must be surrounded by double quotes__*: `!twitch setmessage <twitch_handle> <webhook name> "custom_message"`
* *Requires `administrator` permission in Discord*

#### !twitch getmessage \<twitch handle>
* Gets the currently set custom message for a Twitch channel.
#### !twitch getmessage \<twitch handle> [optional: webhook name]
* Gets the currently set custom message for a Twitch channel for the given Webhook.
If no Webhook name is given, it shows a list of all the custom messages for the Webhooks the channel is tracked in.
* *Requires `administrator` permission in Discord*

#### !twitch preview \<twitch handle> \<webhook name>
* Shows a preview of the live notification for a given channel for the given Webhook.

</details>

Expand Down
11 changes: 5 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ version: "3"
services:
db:
environment:
- POSTGRES_PASSWORD=Pass2020!
- POSTGRES_USER=${PG_USER}
- POSTGRES_PASSWORD=${PG_PWD}
- POSTGRES_DB=${PG_DATABASE}
image: "postgres:11"
restart: unless-stopped
volumes:
Expand All @@ -16,12 +18,9 @@ services:
env_file: secrets.env
environment:
- PG_HOST=db
- PG_DATABASE=esportsbot
- PG_USER=postgres
- PG_PWD=Pass2020!
- ENABLE_TWITTER=False
- ENABLE_TWITCH=False
restart: unless-stopped
ports:
- 443:443

pg_admin:
depends_on:
Expand Down
6 changes: 0 additions & 6 deletions src/esportsbot/base_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
from esportsbot.models import Voicemaster_master, Voicemaster_slave, Guild_info


async def send_to_log_channel(self, guild_id, msg):
db_logging_call = DBGatewayActions().get(Guild_info, guild_id=guild_id)
if db_logging_call.log_channel_id:
await self.bot.get_channel(db_logging_call.log_channel_id).send(msg)


def role_id_from_mention(pre_clean_data: str) -> int:
"""Extracts the ID of a role from a role mention.
Will also accept strings containing a role ID, and will reject invalid integers with a ValueError.
Expand Down
16 changes: 9 additions & 7 deletions src/esportsbot/cogs/AdminCog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from discord.ext import commands
from esportsbot.base_functions import send_to_log_channel
import os

from discord.ext import commands

devs = os.getenv("DEV_IDS").replace(" ", "").split(",")


Expand Down Expand Up @@ -29,11 +29,13 @@ def is_dev(ctx):
@commands.has_permissions(manage_messages=True)
async def clear_messages(self, ctx, amount=5):
await ctx.channel.purge(limit=int(amount) + 1)
await send_to_log_channel(
self,
ctx.author.guild.id,
self.STRINGS['channel_cleared'].format(author_mention=ctx.author.mention,
message_amount=amount)
await self.bot.adminLog(
ctx.message,
{
"Cog": str(type(self)),
"Message": self.STRINGS['channel_cleared'].format(author_mention=ctx.author.mention,
message_amount=amount)
}
)

@commands.command(
Expand Down
46 changes: 31 additions & 15 deletions src/esportsbot/cogs/DefaultRoleCog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from discord.ext import commands
from esportsbot.base_functions import role_id_from_mention
from esportsbot.db_gateway import DBGatewayActions
from esportsbot.models import Guild_info
from esportsbot.base_functions import role_id_from_mention, send_to_log_channel


class DefaultRoleCog(commands.Cog):
Expand All @@ -18,13 +18,23 @@ async def on_member_join(self, member):
if guild.default_role_id:
default_role = member.guild.get_role(guild.default_role_id)
await member.add_roles(default_role)
await send_to_log_channel(
self,
member.guild.id,
f"{member.mention} has joined the server and received the {default_role.mention} role"
await self.bot.adminLog(
None,
{
"Cog": str(type(self)),
"Message": f"{member.mention} has joined the server and received the {default_role.mention} role"
},
guildID=member.guild.id
)
else:
await send_to_log_channel(self, member.guild.id, f"{member.mention} has joined the server")
await self.bot.adminLog(
None,
{
"Cog": str(type(self)),
"Message": f"{member.mention} has joined the server"
},
guildID=member.guild.id
)

@commands.command(
name="setdefaultrole",
Expand All @@ -46,11 +56,15 @@ async def setdefaultrole(self, ctx, given_role_id):

await ctx.channel.send(self.STRINGS['default_role_set'].format(role_id=cleaned_role_id))
default_role = ctx.author.guild.get_role(cleaned_role_id)
await send_to_log_channel(
self,
ctx.author.guild.id,
self.STRINGS['default_role_set_log'].format(author=ctx.author.mention,
role_mention=default_role.mention)
await self.bot.adminLog(
ctx.message,
{
"Cog":
str(type(self)),
"Message":
self.STRINGS['default_role_set_log'].format(author=ctx.author.mention,
role_mention=default_role.mention)
}
)
else:
await ctx.channel.send(self.STRINGS['default_role_set_missing_params'])
Expand Down Expand Up @@ -88,10 +102,12 @@ async def removedefaultrole(self, ctx):
guild.default_role_id = None
DBGatewayActions().update(guild)
await ctx.channel.send(self.STRINGS['default_role_removed'])
await send_to_log_channel(
self,
ctx.author.guild.id,
self.STRINGS['default_role_removed_log'].format(author_mention=ctx.author.mention)
await self.bot.adminLog(
ctx.message,
{
"Cog": str(type(self)),
"Message": self.STRINGS['default_role_removed_log'].format(author_mention=ctx.author.mention)
}
)
else:
await ctx.channel.send(self.STRINGS['default_role_missing'])
Expand Down
22 changes: 13 additions & 9 deletions src/esportsbot/cogs/LogChannelCog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from discord.ext import commands
from esportsbot.base_functions import channel_id_from_mention
from esportsbot.db_gateway import DBGatewayActions
from esportsbot.models import Guild_info
from esportsbot.base_functions import channel_id_from_mention, send_to_log_channel


class LogChannelCog(commands.Cog):
Expand All @@ -22,10 +22,12 @@ async def setlogchannel(self, ctx, given_channel_id=None):
db_item = Guild_info(guild_id=ctx.guild.id, log_channel_id=cleaned_channel_id)
DBGatewayActions().create(db_item)
await ctx.channel.send(self.STRINGS["channel_set"].format(channel_id=cleaned_channel_id))
await send_to_log_channel(
self,
ctx.author.guild.id,
self.STRINGS["channel_set_notify_in_channel"].format(author_mention=ctx.author.mention),
await self.bot.adminLog(
ctx.message,
{
"Cog": str(type(self)),
"Message": self.STRINGS["channel_set_notify_in_channel"].format(author_mention=ctx.author.mention)
}
)
return

Expand All @@ -37,10 +39,12 @@ async def setlogchannel(self, ctx, given_channel_id=None):
guild.log_channel_id = cleaned_channel_id
DBGatewayActions().update(guild)
await ctx.channel.send(self.STRINGS["channel_set"].format(channel_id=cleaned_channel_id))
await send_to_log_channel(
self,
ctx.author.guild.id,
self.STRINGS["channel_set_notify_in_channel"].format(author_mention=ctx.author.mention),
await self.bot.adminLog(
ctx.message,
{
"Cog": str(type(self)),
"Message": self.STRINGS["channel_set_notify_in_channel"].format(author_mention=ctx.author.mention)
}
)

@commands.command(name="getlogchannel", usage="", help="Gets the server logging channel for bot actions")
Expand Down
Loading