0% found this document useful (0 votes)
27 views4 pages

Message

This document outlines a Discord bot implemented using the discord.py library, which tracks the usage of a specified keyword by users. It includes commands for setting the keyword, counting occurrences, managing a leaderboard, and blacklisting users from tracking. The bot also features cooldowns for certain commands and provides feedback through embedded messages in Discord.

Uploaded by

coodcoodie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Message

This document outlines a Discord bot implemented using the discord.py library, which tracks the usage of a specified keyword by users. It includes commands for setting the keyword, counting occurrences, managing a leaderboard, and blacklisting users from tracking. The bot also features cooldowns for certain commands and provides feedback through embedded messages in Discord.

Uploaded by

coodcoodie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import discord

from [Link] import commands


from [Link] import has_permissions, CommandOnCooldown, BucketType
import os
from dotenv import load_dotenv
from collections import defaultdict
import re

load_dotenv()

intents = [Link]()
intents.message_content = True
[Link] = True
bot = [Link](command_prefix='key', intents=intents)

word_to_track = ""
word_counts = defaultdict(int)
user_counts = defaultdict(lambda: defaultdict(int))
blacklisted_users = set()
DEVELOPER_ID = 932538530146713600

@[Link]
async def on_ready():
print(f'Logged in as {[Link]}')

@[Link]
async def on_message(message):
if [Link]:
return

# Match only the exact word


if word_to_track:
words = [Link](r'\b\w+\b', [Link]())
if word_to_track.lower() in words:
word_counts[[Link]] += 1
user_counts[word_to_track.lower()][[Link]] += 1

await bot.process_commands(message)

@[Link]()
@has_permissions(manage_messages=True)
async def setword(ctx, *, word):
global word_to_track
word_to_track = word
embed = [Link](
description=f"Keyword set successfully!",
color=0xc4da9c
)
await [Link](embed=embed)

@[Link]()
@[Link](1, 86400, [Link])
async def count(ctx, member: [Link] = None):
author_id = [Link]
is_admin = [Link].guild_permissions.manage_messages
is_dev = (author_id == DEVELOPER_ID)

if member and not is_admin and not is_dev:


embed = [Link](
description="You do not have permission to check others.",
color=0xff0000
)
return await [Link](embed=embed)

target = member or [Link]


count = word_counts.get([Link], 0)

embed = [Link](
description=f"Keyword count: {count} times",
color=0xf9c901
)
await [Link](embed=embed)

if is_admin or is_dev:
[Link].reset_cooldown(ctx)

@[Link](aliases=["lb"])
@has_permissions(manage_messages=True)
async def leaderboard(ctx):
if not word_to_track:
embed = [Link](description="No keyword is set yet.", color=0xff0000)
return await [Link](embed=embed)

user_data = user_counts[word_to_track.lower()]
sorted_users = sorted(user_data.items(), key=lambda x: x[1], reverse=True)[:10]

leaderboard_text = ""
for i, (user_id, count) in enumerate(sorted_users, start=1):
member = [Link].get_member(user_id)
name = member.display_name if member else f"<@{user_id}>"
leaderboard_text += f"{i}. {name} — {count} times\n"

embed = [Link](
title="Leaderboard",
description=leaderboard_text or "No data yet.",
color=0xffffff
)
await [Link](embed=embed)

@[Link]
async def count_error(ctx, error):
if isinstance(error, CommandOnCooldown):
embed = [Link](
description=f"You can use this command again <t:{int(error.retry_after
+ [Link].created_at.timestamp())}:R>",
color=0xff0000
)
await [Link](embed=embed)

@[Link]()
@has_permissions(manage_messages=True)
async def reset(ctx, target: str):
if [Link]() == "all":
word_counts.clear()
user_counts[word_to_track.lower()].clear()
embed = [Link](
description="All user keyword counts have been reset.",
color=0xc4da9c
)
return await [Link](embed=embed)

if [Link]:
user = [Link][0]
word_counts[[Link]] = 0
user_counts[word_to_track.lower()][[Link]] = 0
embed = [Link](
description=f"{[Link]}'s keyword count has been reset.",
color=0xc4da9c
)
return await [Link](embed=embed)

embed = [Link](
description="Please mention a user or use `!reset all`.",
color=0xff0000
)
await [Link](embed=embed)

# ===== Blacklist Commands =====

@[Link](name="bl")
@has_permissions(manage_messages=True)
async def blacklist_user(ctx, member: [Link]):
blacklisted_users.add([Link])
embed = [Link](
description=f"{[Link]} has been blacklisted from keyword
tracking.",
color=0xff0000
)
await [Link](embed=embed)

@[Link](name="ubl")
@has_permissions(manage_messages=True)
async def unblacklist_user(ctx, member: [Link]):
if [Link] in blacklisted_users:
blacklisted_users.remove([Link])
embed = [Link](
description=f"{[Link]} has been removed from the blacklist.",
color=0xc4da9c
)
else:
embed = [Link](
description=f"{[Link]} is not blacklisted.",
color=0xff0000
)
await [Link](embed=embed)

@[Link](name="blacklistlist")
@has_permissions(manage_messages=True)
async def blacklist_list(ctx):
if not blacklisted_users:
embed = [Link](description="No users are blacklisted.",
color=0xff0000)
return await [Link](embed=embed)

list_text = ""
for user_id in blacklisted_users:
member = [Link].get_member(user_id)
name = member.display_name if member else f"<@{user_id}>"
list_text += f"- {name}\n"

embed = [Link](
title="Blacklisted Users",
description=list_text,
color=0xff0000
)
await [Link](embed=embed)

# DEBUG LINE TO CHECK TOKEN


print("TOKEN:", [Link]("BOT_TOKEN"))

# RUN THE BOT


[Link]([Link]("BOT_TOKEN"))

You might also like