Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f445aef
Start of new cog: Insight
t1ngs0n Nov 16, 2020
1a90902
Code for initialisation added
t1ngs0n Nov 16, 2020
f5fb971
Added functionality of k!insights and k!servers
t1ngs0n Dec 14, 2020
1413378
Added comments to Insights
t1ngs0n Dec 14, 2020
1c8ace8
Added to CHANGELOG.md
t1ngs0n Dec 27, 2020
0d88aeb
Merge branch 'master' into Insights
t1ngs0n Dec 27, 2020
ca97f32
Add tests for insights and servers commands
Unknownlocal Apr 5, 2021
bd2ae8f
Add tests for insights and servers commands
Unknownlocal Apr 5, 2021
c5ffd90
Merge remote-tracking branch 'origin/Insights' into Insights
Unknownlocal Apr 5, 2021
d0350d3
Fix issues with test_Insights.py caused by random merge conflicts
Unknownlocal Apr 5, 2021
68b29a9
Account for fake bot user in test_insights()
Unknownlocal Apr 5, 2021
502cf7c
Remove bot user addition from test_insights()
Unknownlocal Apr 5, 2021
0456e45
Attempt moving configure to inside test method
Unknownlocal Apr 5, 2021
c144bfa
Attempt moving configure to inside test method
Unknownlocal Apr 5, 2021
82ac0c5
Refactor test_insights()
Unknownlocal Apr 5, 2021
64b269a
Remove DBManager stuff from another conflict
Unknownlocal Apr 5, 2021
612ee4b
Merge remote-tracking branch 'origin/master' into Insights
Unknownlocal Jun 9, 2021
dac39d9
Merge remote-tracking branch 'origin/master' into Insights
Unknownlocal Jun 17, 2021
21c2d9c
Implement k!insights test
Unknownlocal Jun 28, 2021
1d33ac7
Implement k!insights test
Unknownlocal Jul 26, 2021
760d7a7
Remove broken k!servers tests
Unknownlocal Jul 26, 2021
df35c50
Fix previous broken commits with patched dpytest
Unknownlocal Oct 11, 2021
83d743e
Merge remote-tracking branch 'origin/master' into Insights
Unknownlocal Nov 29, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ A lot of these commands will only be available to administrators
- Fix regex incorrectly being used on some messages


### Insights
#### Added
- `insights` Displays the insights for KoalaBot
- `servers <text>` Displays the servers that the bot is in with the given text in their name. (If left blank, displays all servers)

## [0.2.0] - 15-10-2020
### Text Filter
##### Added
Expand Down
94 changes: 94 additions & 0 deletions cogs/Insights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env python

"""
Koala Bot Insights Code
Created by: Samuel Tiongson
"""
from discord.ext import commands
import KoalaBot


class Insights(commands.Cog):
"""
A discord.py cog pertaining to showing the insights of the KoalaBot
"""

def __init__(self, bot, database_manager=None):
self.bot = bot

@commands.check(KoalaBot.is_owner)
@commands.command(name="insights", aliases=[])
async def insights(self, ctx):
"""
Returns the number of servers KoalaBot is in and the total number of members of those servers.

:param ctx: The discord context
"""
guilds = self.bot.guilds
number_of_servers = len(guilds)
number_of_members = 0

for guild in guilds:
number_of_members += guild.member_count

await ctx.send(f"KoalaBot is in {number_of_servers} servers with a member total of {number_of_members}.")

@commands.check(KoalaBot.is_owner)
@commands.command(name="servers", aliases=[])
async def servers(self, ctx, *, arg=None):
"""
Returns the names of the servers KoalaBot is in

:param ctx: The discord context
:param arg: Searches for guilds with argument provided
"""
# Retrieves AsyncIterator
guild_list = self.bot.fetch_guilds()
guild_list_names = []

# Cycle through iterator, check if there is an arg, if there is check against the arg, if not just append the
# guild name
async for guild in guild_list:
if arg is not None:
# Change arg and guild name to uppercase, and split guild name by spaces
if arg.upper() in guild.name.upper().split(" "):
guild_list_names.append(guild.name)
else:
guild_list_names.append(guild.name)

# If there are no guilds and no arguments
if len(guild_list_names) == 0 and arg is None:
await ctx.send("KoalaBot is in no servers!")

# If there are no guilds but there are arguments
elif len(guild_list_names) == 0 and arg is not None:
await ctx.send(f"No servers with {arg} in their name!")

# There must be guilds in the list
else:
string_to_send = ''
# While there are guilds in the list, run code
while len(guild_list_names) != 0:
# Get the length of the first server name
length = len(guild_list_names[0])
# If this length + the current string length + 2 for comma and space is greater than 2000
if len(string_to_send) + length + 2 > 2000:
# Print the string and reset it to nothing
await ctx.send(string_to_send)
string_to_send = ''
else:
# If the above is not true, then pop the server name from the list and add it to the string
guild = guild_list_names.pop(0)
string_to_send += guild + ", "

# Remove the comma and space at the end of the string
await ctx.send(string_to_send[:-2])


def setup(bot: KoalaBot) -> None:
"""
Load this cog to the KoalaBot.
:param bot: the bot client for KoalaBot
"""
bot.add_cog(Insights(bot))
print("Insights is ready.")