generated from EndstoneMC/python-example-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: send join and leave messages to channel
- Loading branch information
1 parent
bfa37cf
commit 1fb0478
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import multiprocessing | ||
|
||
import discord | ||
from discord.ext import tasks | ||
|
||
|
||
def main(queue: multiprocessing.SimpleQueue, config: dict): | ||
intents = discord.Intents.default() | ||
intents.message_content = True | ||
intents.members = True | ||
client = discord.Client(queue=queue, config=config, intents=intents) | ||
|
||
@tasks.loop(seconds=1) | ||
async def main_loop(channels: dict[str, discord.TextChannel]): | ||
while not queue.empty(): | ||
msg = queue.get() | ||
event, data = msg["event"], msg["data"] | ||
match event: | ||
case "join": | ||
player_name = data["player_name"] | ||
if "join" in channels: | ||
await channels["join"].send(f"{player_name} joined the server!") | ||
case "leave": | ||
player_name = data["player_name"] | ||
if "leave" in channels: | ||
await channels["leave"].send(f"{player_name} left the server!") | ||
|
||
@client.event | ||
async def on_ready() -> None: | ||
channels = { | ||
channel_type: client.get_channel(int(channel_id)) for channel_type, channel_id in config["channels"].items() | ||
} | ||
print(channels) | ||
main_loop.start(channels) | ||
|
||
client.run(token=config["token"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
token = "PUT_YOUR_DISCORD_TOKEN_HERE" | ||
|
||
[channels] | ||
#join = "CHANNEL_ID" | ||
#leave = "CHANNEL_ID" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,48 @@ | ||
import multiprocessing | ||
import os | ||
import sys | ||
|
||
from endstone.event import event_handler, PlayerJoinEvent, PlayerQuitEvent | ||
from endstone.plugin import Plugin | ||
|
||
from endstone_discord.client import main | ||
|
||
if sys.platform == "win32": | ||
multiprocessing.set_executable(os.path.join(sys.prefix, "python.exe")) | ||
else: | ||
multiprocessing.set_executable(os.path.join(sys.prefix, "bin", "python")) | ||
|
||
|
||
class DiscordPlugin(Plugin): | ||
api_version = "0.4" | ||
|
||
def on_enable(self): | ||
self.save_default_config() | ||
self.register_events(self) | ||
|
||
self._queue = multiprocessing.SimpleQueue() | ||
self._process = multiprocessing.Process(target=main, args=(self._queue, self.config)) | ||
self._process.start() | ||
|
||
def on_disable(self): | ||
if self._process.is_alive(): | ||
self._process.terminate() | ||
self._process.join() | ||
|
||
@event_handler | ||
def on_player_join(self, event: PlayerJoinEvent) -> None: | ||
self._queue.put( | ||
{ | ||
"event": "join", | ||
"data": {"player_name": event.player.name}, | ||
} | ||
) | ||
|
||
@event_handler | ||
def on_player_quit(self, event: PlayerQuitEvent) -> None: | ||
self._queue.put( | ||
{ | ||
"event": "leave", | ||
"data": {"player_name": event.player.name}, | ||
} | ||
) |